- Home /
how to find a point(vector2) between two points(Vector2) of a line (line renderer)
Hii Everyone, As the question explains, how to find a point(vector2) between two points(Vector2) of a line (line renderer),
Here is the script for my line intersection check! I am using a line renderer to draw the line with two points (start with vector and endpoint), what I want is that want point A in the line as you can see in the image.
In theory, I want to shorten the line from the original length for my use case, I am doing a line intersection check for two lines overlapping, but the problem is that in some cases the lines start from the same point, and in those situations if the points meet, it still counts as a line intersection. So in theory I want to shorten the line for line intersection check so it doesn't intersect with the other line if the points meet.
public bool isIntersecting = false;
LineRenderer lineRenderer;
private void Start()
{
lineRenderer = this.GetComponent<LineRenderer>();
}
private void Update()
{
LineRenderer recievedLine;
if (FindObjectOfType<LineDynamics>().ropeRenderer != null)
{
recievedLine = FindObjectOfType<LineDynamics>().ropeRenderer;
}
else
{
return;
}
AreLineSegmentsIntersectingDotProduct(lineRenderer.GetPosition(0), lineRenderer.GetPosition(lineRenderer.positionCount - 1), recievedLine.GetPosition(0), recievedLine.GetPosition(recievedLine.positionCount - 1));
}
//Line segment-line segment intersection in 2d space by using the dot product
//p1 and p2 belongs to line 1, and p3 and p4 belongs to line 2
public bool AreLineSegmentsIntersectingDotProduct(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
{
if (IsPointsOnDifferentSides(p1, p2, p3, p4) && IsPointsOnDifferentSides(p3, p4, p1, p2))
{
isIntersecting = true;
}
else
{
isIntersecting = false;
}
return isIntersecting;
}
//Are the points on different sides of a line?
private bool IsPointsOnDifferentSides(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4)
{
bool isOnDifferentSides = false;
//The direction of the line
Vector2 lineDir = p2 - p1;
//The normal to a line is just flipping x and z and making z negative
Vector2 lineNormal = new Vector2(-lineDir.y, lineDir.x);
//Now we need to take the dot product between the normal and the points on the other line
float dot1 = Vector2.Dot(lineNormal, p3 - p1);
float dot2 = Vector2.Dot(lineNormal, p4 - p1);
//If you multiply them and get a negative value then p3 and p4 are on different sides of the line
if (dot1 * dot2 < 0f)
{
isOnDifferentSides = true;
}
return isOnDifferentSides;
}
Answer by Llama_w_2Ls · Feb 28, 2021 at 01:35 PM
To find a point on that line, you use the formula: Vector2 point = startPoint + endPoint * distance
, where startPoint and endPoint are Vector2's, and distance is a float, which is the distance between the startPoint and the desired point across the line.
Therefore, this equation when distance is equal to the length of the whole line, would return the endpoint. Try using this script to visualize it for yourself:
public float Distance;
public Vector2 pointAcrossLine;
public Vector2 StartPoint;
public Vector2 EndPoint;
void Update()
{
// Makes sure the point is on the line
float maxDistance = Vector2.Distance(StartPoint, EndPoint);
Distance = Mathf.Clamp(Distance, 0f, maxDistance);
pointAcrossLine = StartPoint + EndPoint * Distance;
}
Answer by felres · Feb 09 at 05:12 AM
Not the solution, but I had the same problem regarding checking intersecting lines on their origin. The way I solved it was checking if none of the 4 points was equal to one another. So for example in my code I have this:
// assume line 1 goes from n1 to n2
// assume line 2 goes from n3 to n4
bool intersect = DoLineSegmentsIntersect(n1, n2, n3, n4);
bool originIsShared = ( (n1==n2)|| (n1==n3) || (n1==n4) || (n2==n3) || (n2==n4) || (n3==n4) );
if (intersect && !originIsShared) return true;