Click here to Skip to main content
15,909,530 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionProblem with PropertySheet size.... Pin
klvin2-Feb-06 22:31
klvin2-Feb-06 22:31 
AnswerRe: Problem with PropertySheet size.... Pin
David Crow3-Feb-06 4:34
David Crow3-Feb-06 4:34 
Questionpolyline Pin
Russell'2-Feb-06 22:23
Russell'2-Feb-06 22:23 
AnswerRe: polyline Pin
_anil_2-Feb-06 22:27
_anil_2-Feb-06 22:27 
GeneralRe: polyline Pin
Russell'2-Feb-06 22:48
Russell'2-Feb-06 22:48 
GeneralRe: polyline Pin
_anil_2-Feb-06 23:54
_anil_2-Feb-06 23:54 
GeneralRe: polyline Pin
Russell'3-Feb-06 0:15
Russell'3-Feb-06 0:15 
AnswerRe: polyline Pin
Iain Clarke, Warrior Programmer3-Feb-06 0:55
Iain Clarke, Warrior Programmer3-Feb-06 0:55 
Your going to like me....

The very clever stuff was taken almost exactly from http://www.faqs.org/faqs/graphics/algorithms-faq/ which is a superb repository of this sort of stuff.

I'm sure you could make a more compact version of this stuff, but many of these functions are used elsewhere in my code, so it made sense to keep them multi-purpose.

ptPoly is an array of the vertices of the polygon.

BOOL	PointInPolygon (const CPoint *ptPoly, const int nPoints, const CPoint ptTest)
{
	return (LineCrossesPolygon (ptPoly, nPoints, CPoint (-99999999, ptTest.y), ptTest) % 2) == 1;
}

int		LineCrossesPolygon (const CPoint *ptPoly, const int nPoints, const CPoint ptA1, const CPoint ptA2)
{
	int nPt, nCross = 0;

	for (nPt = 0; nPt < nPoints; nPt++)
	{
		if (LinesIntersect (ptA1, ptA2, ptPoly [nPt], ((nPt + 1) == nPoints) ? ptPoly [0] : ptPoly [nPt + 1], NULL))
			nCross++;
	}

	return nCross;
}

/*
From http://www.faqs.org/faqs/graphics/algorithms-faq/

	        (Ay-Cy)(Dx-Cx)-(Ax-Cx)(Dy-Cy)
        r = -----------------------------  (eqn 1)
            (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)

            (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
        s = -----------------------------  (eqn 2)
            (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)

If 0<=r<=1 & 0<=s<=1, intersection exists
            r<0 or r>1 or s<0 or s>1 line segments do not intersect

        If the denominator in eqn 1 is zero, AB & CD are parallel
        If the numerator in eqn 1 is also zero, AB & CD are collinear.



*/

// Returns true iff ab properly intersects cd: they share a point interior to both segments.
// The properness of the intersection is ensured by using strict leftness.
BOOL	LinesIntersect (CPoint a, CPoint b, CPoint c, CPoint d, CPoint *ptIntersect)
{
	// Quick check for parallel
	if (Colinear (a,b,c,d))
		return FALSE;

	// Get the "meeting point" of the two lines.
	double rTop, rBot, sTop, sBot, r, s;
	double Ax = a.x, Ay = a.y, Bx = b.x, By = b.y, Cx = c.x, Cy = c.y, Dx = d.x, Dy = d.y;
	rTop = (Ay-Cy)*(Dx-Cx)-(Ax-Cx)*(Dy-Cy);
	rBot = (Bx-Ax)*(Dy-Cy)-(By-Ay)*(Dx-Cx);
	sTop = (Ay-Cy)*(Bx-Ax)-(Ax-Cx)*(By-Ay);
	sBot = rBot;

	if (sBot == 0.0)
		return FALSE; // Parallel.
	r = rTop / rBot;
	if (r < 0.0)
		return FALSE;
	if (r > 1.0)
		return FALSE;

	s = sTop / sBot;
	if (s < 0.0)
		return FALSE;
	if (s > 1.0)
		return FALSE;

	if (ptIntersect)
		*ptIntersect = InterpolatePoint (a, b, r, 1.0);

	return TRUE;
}

BOOL Colinear(CPoint a, CPoint b, CPoint c, CPoint d)
{
	int dot = (b.x-a.x)*(d.y-c.y) - (d.x-c.x)*(b.y-a.y);
	return !dot;
//	int CrossZ = (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
//	return (abs(CrossZ) < 1);
}

CPoint	InterpolatePoint (CPoint Begin, CPoint End, double Which, double OutOf)
{
	CPoint pt;
	pt.x = (int)InterpolateDouble (Begin.x, End.x, Which, OutOf);
	pt.y = (int)InterpolateDouble (Begin.y, End.y, Which, OutOf);
	return pt;
}
double	InterpolateDouble ( double Begin, double End, double Which, double OutOf)
{
	if (OutOf == 0.0)
		return	Begin;
	return	((End - Begin) * Which / OutOf) + Begin;
}



Good luck!

Iain.
GeneralRe: polyline Pin
Russell'3-Feb-06 1:48
Russell'3-Feb-06 1:48 
GeneralRe: polyline Pin
_anil_5-Feb-06 14:07
_anil_5-Feb-06 14:07 
QuestionDialog Information not showing in class wizard Pin
BiswaR2-Feb-06 22:12
BiswaR2-Feb-06 22:12 
AnswerRe: Dialog Information not showing in class wizard Pin
_anil_2-Feb-06 22:39
_anil_2-Feb-06 22:39 
GeneralRe: Dialog Information not showing in class wizard Pin
BiswaR2-Feb-06 23:05
BiswaR2-Feb-06 23:05 
GeneralRe: Dialog Information not showing in class wizard Pin
_anil_2-Feb-06 23:47
_anil_2-Feb-06 23:47 
GeneralRe: Dialog Information not showing in class wizard Pin
Iain Clarke, Warrior Programmer3-Feb-06 0:57
Iain Clarke, Warrior Programmer3-Feb-06 0:57 
QuestionHard Disk Drive Information Pin
Anilkumar K V2-Feb-06 21:47
Anilkumar K V2-Feb-06 21:47 
AnswerRe: Hard Disk Drive Information Pin
Owner drawn2-Feb-06 22:22
Owner drawn2-Feb-06 22:22 
Question[Message Deleted] Pin
yamunasenthilvel2-Feb-06 21:28
yamunasenthilvel2-Feb-06 21:28 
AnswerRe: class in multithreading Pin
Owner drawn2-Feb-06 21:31
Owner drawn2-Feb-06 21:31 
General[Message Deleted] Pin
yamunasenthilvel2-Feb-06 22:13
yamunasenthilvel2-Feb-06 22:13 
GeneralRe: class in multithreading Pin
Owner drawn2-Feb-06 22:21
Owner drawn2-Feb-06 22:21 
Questionfractal Pin
dokhtarakekebritfroosh2-Feb-06 21:09
dokhtarakekebritfroosh2-Feb-06 21:09 
JokeRe: fractal Pin
toxcct2-Feb-06 22:38
toxcct2-Feb-06 22:38 
GeneralRe: fractal Pin
Prakash Nadar2-Feb-06 23:42
Prakash Nadar2-Feb-06 23:42 
GeneralRe: fractal Pin
toxcct2-Feb-06 23:43
toxcct2-Feb-06 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.