Determine whether point P is above or below a vector through points A, B
Possible values for :
0 On the vector
> 0 Above the vector
< 0 Below the vector
The function above is the ‘2D cross product’, therefore z is the surface of vector and . Dividing the surface by the length of vector () gives the ‘signed distance’ of the point perpendicular to the point .
Determine the distance of point P and vector through points P1, P2
This is a ‘2D cross product’ of and . Since the cross product is the parallelogram surface of the two vectors, dividing it by the length of to get the distance.
To check whether two 2D lines are coincident, the distance of and to need to be calculated. For a coincident line both need to be (almost) zero.
The z component of the cross vector can be used for this quite elegantly.
Note: The cross vector is noted as an x e.g. x
To check whether the two lines and are coincident, the z-component of two cross vectors need to be calculated. Both calculate the distance of the points on line to .
x is the area of the parallelogram , dividing it with the length of () gives the shortest distance of point to line .
x gives the area of the parallelogram
After dividing with this is also the shortest distance of to line . If both distances are small enough the lines are coincident.
The cross product of two parallel direction vectors has zero length. The magnitude (length) of the cross product is the area enclosed by the vectors. Two parallel direction vectors obviously do not have a surface since they can be considered the same origin.
In 2D the z axis is the normal to the x and y axis. The z-axis is calculated by using the cross vector of both vectors.
The cross vector Z-component can be calculated using:
In this note I will explain how to find the intersection point P between two line segments. Note that this method will also calculate intersections on extended line segments.
As a reminder the cross product is the area of the parallelogram enclosed by the two As a reminder, the cross product is the area of the parallelogram enclosed by the two vectors. In 2D graphics, we will calculate only the z-component of the cross-vector, which will be called the cross-product in this note.
It can be calculated using the following formula:
Calculating the actual intersection:
As an example we will calculate the intersection point of line segments and as shown in image 1.
We will consider the line segments as vectors, this gives us the following vectors.
,
To calculate the intersection point we will first calculate the area of the parallelogram formed by AB and CD as shown in image 2.
The area can be calculated using the cross product of
Calculating the offset on segment
We will now calculate the area below vector as seen in image 3
It can be seen the offset on segment on will be equal to this area divided by the total area calculated earlier. Fortunately we can easily calculate the area by using the area shown in image 4.
The areas shown in image 3 and image 4 are the same. Note that image 4 shows the parallelogram formed by and , we will use the cross-product to calculate it.
We will introduce vector for this.
Now we are almost done. We have both areas (9 and 36) so we can create the offset
which can be simplified to
If we multiply the offset with we find the point on the vector .
Since the line segment does not start on the actual point needs to be moved by
Let’s check whether it is correct
As a general formula:
The offset on can be negative or larger then one. In that case the intersection is on the extension of line segment . This is an advantage of this method.
Of course the line intersection method can be used but in case of horizontal or vertical lines a quicker solution is available.
Assume the vector V1 defined by points P1 and P2. and the horizontal line is through point P
The offset of the intersection is:
Lets say P1 is 10,10 P2 20,20 and P.y 0,15
In this case (15-10)/(20-10) is 0.5
If P1 and P2 is revered it will be (15-20)/(10-20) = -5/-10 = 0.5.
Note: There is a special case if the vector is parallel or on the horizontal line, in that case, there is no intersection. In that case, p2.y – p1y is zero. This situation must be checked since it will also prevent a division by zero. You probably also want to limit very small values of this value since it may result in very large (positive or negatively) offset values.
Vertical lines: In case of a vertical line replace all .y above by x.
Calculating the intersection point: The offset when the vector is hit is always >= 0.0 and <= 1.0, otherwise, it is on the extended vector.
Calculating the intersection point on the(extended) vector is trivial