Click here to Skip to main content
15,907,910 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Linux C++ implementation of ioctl Pin
leon de boer26-Mar-18 16:53
leon de boer26-Mar-18 16:53 
GeneralRe: Linux C++ implementation of ioctl Pin
Vaclav_4-Apr-18 5:08
Vaclav_4-Apr-18 5:08 
QuestionVS2017: Cannot debug Dll's Pin
indrekm23-Mar-18 1:13
indrekm23-Mar-18 1:13 
SuggestionRe: VS2017: Cannot debug Dll's Pin
Richard MacCutchan23-Mar-18 1:21
mveRichard MacCutchan23-Mar-18 1:21 
SuggestionRe: VS2017: Cannot debug Dll's Pin
Jochen Arndt23-Mar-18 1:25
professionalJochen Arndt23-Mar-18 1:25 
AnswerRe: VS2017: Cannot debug Dll's Pin
Randor 23-Mar-18 8:09
professional Randor 23-Mar-18 8:09 
QuestionSelect and Pick lines in OpenGL Pin
Gopi Nath22-Mar-18 19:40
Gopi Nath22-Mar-18 19:40 
AnswerRe: Select and Pick lines in OpenGL Pin
leon de boer22-Mar-18 20:30
leon de boer22-Mar-18 20:30 
Find the line in your list that is closest the point or at least less than some tolerance value
A number of Contributed implementations of the formula here plus an explaination of the mathematics
Point, Line, Plane[^]

Implementation would look something like this
#define GLfloat float	// No sure if you want floats or double you can change here
/* Calculate euclidian distance between vertex points (x1,y1) and (x2,y2) */
GLfloat GLDistance2D (GLfloat x1, GLfloat y1,						// Vertex 1 (x1,y1)
							GLfloat x2, GLfloat y2)					// Vertex 2 (x2,y2)
{
	GLfloat dx, dy;
	dx = x2 - x1;													// Diff x
	dy = y2 - y1;													// Diff y
	if ( dx < -FLT_EPSILON || dx > FLT_EPSILON ||
		 dy < -FLT_EPSILON || dy > FLT_EPSILON )					// Check dx, dy or both are non zero 
	{
		return sqrtf(dx * dx + dy * dy);							// Calculate the distance	
	}
	return (GLfloat) 0.0;											// Both dx and dy are zero
}


/* Given (px, py) and two points forming line segment(x1, y1), (x2, y2).
   Calculate the closest point(cpx, cpy) between the point and the line and
   return the distance.The linesegment flag is true the closest point must be
   on the line segment or false it does an extended infinite line test. */
GLfloat GLDistanceToLine2D (GLfloat px, GLfloat py,					// Test point (px,py)
							GLfloat x1, GLfloat y1,					// Line vertex 1 (x1,y1)
							GLfloat x2, GLfloat y2,					// Line vertex 2 (x2,y2)
							bool linesegment,						// Flag true if line segment, false extended line								
							GLfloat* cpx, GLfloat* cpy)			// Closest point point result pointers
{
	GLfloat u;														// see Paul Bourke's original article(s)
	GLfloat dx = x2 - x1;											// Diff x
	GLfloat dy = y2 - y1;											// Diff y
	GLfloat SqLineMag = dx * dx + dy * dy;							// Square of line magnitude
	if (SqLineMag > -FLT_EPSILON && SqLineMag < FLT_EPSILON) {		// We have a single point there is no measurable distance between vertexes 
		if (linesegment) return GLDistance2D(px, py, x1, y1);		// Line segement test return distance between 2 pts
		 else return (GLfloat)-1.0;									// Extended line invalid return -1 as it's impossible
	}
	u = ((px - x1)* dx + (py - y1)* dy) / SqLineMag;				// Calculate value u											
	if ((linesegment) && ((u < FLT_EPSILON) || (u > (GLfloat)1.0)))	// Line segment test and closest point is outside the segment
	{
		GLfloat ix, iy;
		ix = GLDistance2D(px, py, x1, y1);							// Distance line point (x1,y1) to point (px,py)
		iy = GLDistance2D(px, py, x2, y2);							// Distance line point (x2,y2) to point (px,py)
		if (ix < iy) {												// If first distance less than second												
			if (cpx) (*cpx) = x1;									// If x intersect result needed return x1
			if (cpy) (*cpy) = y1;									// If y intersect result needed return y1
			return(ix);												// Return distance as calculated distance ix
		}
		else {
			if (cpx) (*cpx) = x2;									// If x intersect result needed return x2
			if (cpy) (*cpy) = y2;									// If y intersect result needed return y2
			return(iy);												// Return distance as calculated distance iy
		}
	}
	else {															// Closest point is between line points or extended line test	
		GLfloat ix, iy;
		ix = x1 + (u * dx);											// Calculate intersection point x
		if (cpx) (*cpx) = ix;										// If x intersect result needed return it
		iy = y1 + (u * dy);											// Calculate intersection point y
		if (cpy) (*cpy) = iy;										// If y intersect result needed return it
		return GLDistance2D(px, py, ix, iy);						// Return the distance
	}
}


Run a test .. it should return ix,iy = (200,100) dist = 100 ... plot it you will see why.
GLfloat ix, iy;
GLfloat dist = GLDistanceToLine2D(100, 100, 200, 0, 200, 200, true, &ix, &iy);

In vino veritas


modified 23-Mar-18 2:36am.

GeneralRe: Select and Pick lines in OpenGL Pin
Gopi Nath22-Mar-18 21:08
Gopi Nath22-Mar-18 21:08 
GeneralRe: Select and Pick lines in OpenGL Pin
Gopi Nath24-Jul-18 21:17
Gopi Nath24-Jul-18 21:17 
QuestionNotifiation Messages for CSpinButtonCtrl in MFC Pin
Sampath57922-Mar-18 3:46
Sampath57922-Mar-18 3:46 
AnswerRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Jochen Arndt22-Mar-18 4:32
professionalJochen Arndt22-Mar-18 4:32 
GeneralRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Sampath57922-Mar-18 7:31
Sampath57922-Mar-18 7:31 
GeneralRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Victor Nijegorodov22-Mar-18 8:37
Victor Nijegorodov22-Mar-18 8:37 
GeneralRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Sampath57922-Mar-18 17:10
Sampath57922-Mar-18 17:10 
GeneralRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Victor Nijegorodov22-Mar-18 21:45
Victor Nijegorodov22-Mar-18 21:45 
GeneralRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Jochen Arndt22-Mar-18 9:46
professionalJochen Arndt22-Mar-18 9:46 
GeneralRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Sampath57922-Mar-18 17:17
Sampath57922-Mar-18 17:17 
GeneralRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Victor Nijegorodov22-Mar-18 21:52
Victor Nijegorodov22-Mar-18 21:52 
GeneralRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Sampath57923-Mar-18 0:22
Sampath57923-Mar-18 0:22 
GeneralRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Jochen Arndt22-Mar-18 22:00
professionalJochen Arndt22-Mar-18 22:00 
GeneralRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Sampath57923-Mar-18 0:22
Sampath57923-Mar-18 0:22 
AnswerRe: Notifiation Messages for CSpinButtonCtrl in MFC Pin
Victor Nijegorodov22-Mar-18 4:38
Victor Nijegorodov22-Mar-18 4:38 
QuestionQR CODE IN ARABIC Pin
Member 1054250021-Mar-18 6:13
Member 1054250021-Mar-18 6:13 
AnswerRe: QR CODE IN ARABIC Pin
Richard MacCutchan21-Mar-18 6:44
mveRichard MacCutchan21-Mar-18 6:44 

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.