Determine whether the vertex is on the online segment (implemented in C ++)

Source: Internet
Author: User

Determine whether the vertex is on the online segment:

If the vertex is set to Q and the line segment is P1P2, the basis for determining the vertex Q on this line segment is: (q-P1) × (P2-P1) = 0 and Q is in P1, p2 is inside the rectangle of the diagonal vertex. The former ensures that the qpoint is on the P1P2 line, and the latter ensures that the qpoint is not on the extended or reverse extended line of the P1P2 line. The following process can be used to determine this step:

On-segment (PI, PJ, PK)

If min (XI, XJ) <= XK <= max (XI, XJ) and min (Yi, YJ) <= yk <= max (Yi, YJ)

Then return true;

Else return false;

Note that, due to the special situations of horizontal and vertical line segments, min (XI, XJ) <= XK <= max (XI, XJ) and min (Yi, YJ) <= yk <= max (Yi, YJ) conditions must be met at the same time to return the true value.

BOOL CGe: PtInLine (double x, double y, double z,
Double x1, double y1, double z1,
Double x2, double y2, double z2)
{// From http://blog.csdn.net/hoya5121
// Determine whether the target point is an endpoint
If (DoubleComp (x, x1) = 0 & DoubleComp (y, y1) = 0 & DoubleComp (z, z1) = 0)
{
Return TRUE;
}
If (DoubleComp (x, x2) = 0 & DoubleComp (y, y2) = 0 & DoubleComp (z, z2) = 0)
{
Return TRUE;
}
//
Double D = distpt2line (X, Y, Z, X1, Y1, Z1, X2, Y2, Z2 );

If (doublecomp (D, 1e-6)> 0) // (D> 1e-6)
{
Return false;
}

If (DoubleComp (x1, x2 )! = 0) // (x1! = X2)
{
If (DoubleComp (x-x1), 0) <0) & (DoubleComp (x-x2), 0)> 0) |
(DoubleComp (x-x1), 0)> 0) & (DoubleComp (x-x2), 0) <0 )))
{
Return TRUE;
}
}
If (DoubleComp (y1, y2 )! = 0) // (y1! = Y2)
{
If (DoubleComp (y-y1), 0) <0) & (DoubleComp (y-y2), 0)> 0) |
(DoubleComp (y-y1), 0)> 0) & (DoubleComp (y-y2), 0) <0 )))
{
Return TRUE;
}
}
If (DoubleComp (z1, z2 )! = 0)
{
If (DoubleComp (z-z1), 0) <0) & (DoubleComp (z-z2), 0)> 0) |
(Doublecomp (z-z1), 0)> 0) & (doublecomp (z-z2), 0) <0 )))
{
Return true;
}
}

Return false;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.