Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Logic for shortest distance between two lines

$
0
0
I am not looking for the shortest distance to clarify. I am looking for the "shortest distance" between two lines in a direction: up, down, left and right. For example, if I have a point (p1) at (0,1) and my line segment define by [(0,0),(2,2)] the distance that point (0,1) see looking right is just 1 - 0, which is the x value of the point on the line segment when y is 1 minus the x value of p1. However the shortest distance is less than 1 running perpendicular to the slope of the line segment. I have came up 35 different variations of endpoint calculations to find the shortest distance between two line. I was wondering if there is a shorter way. These cases are only for when V is looking to the right where L exist.

Here is some sample code but not all of the code.
 if (L.im == 1 && V.im == 1)
	{
		//two vertical lines
		printf("Shortest distance is: %f\n", (float)(L.p0.x - V.p0.x));
		//Figure 1
	}
	else if (L.im == 1 && V.im == 0)
	{
		if (L.p1.y > L.p0.y) //point 1 is higher than point 0
		{
			if (L.p0.y <= V.p0.y && V.p0.y <= L.p1.y && L.p0.y <= V.p1.y && V.p1.y <= L.p1.y)
			{
				//vertices are within points of line
				printf("Shortest distance is: %f\n", min(L.p0.x - V.p0.x,L.p0.x - V.p1.x));
				//this case accounts for when V.p0.y = V.p1.y or slope of vertex line is zero
				//Figure 2
			}
			else if (V.p1.y > V.p0.y) // assume that else if removes the need of the full check logic: (V.p1.y > V.p0.y and V.p0.y <  L.p1.y)
			{
				if(L.p0.y <= V.p0.y && V.p0.y <= L.p1.y)
				{
					//"top" case
					printf("Shortest distance is: %f\n", min(L.p0.x - V.p0.x,L.p1.x - eval(V,L.p1.y)));
					//eval fuction evaluates the equation of any line struct with a given y coordinate and returns a x coordinate
					//Figure 3


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>