A Simple Method for line segment picking and judgment in the drawing program

Source: Internet
Author: User


Author: Zhu Jincan

Source: http://blog.csdn.net/clever101


Generally, in the drawing program, it is relatively simple to pick up the rectangle and the polygon. For example, in the crect and crgn classes of MFC, The ptinrect or ptinrgn function directly determines whether the vertex is in the graph. We pay attention to the way we pick up a line segment, because we generally use the width of a pixel to draw a straight line, it is difficult for the user to directly click the online segment (it is estimated that the user may not be able to directly click the online segment multiple times ). A common practice is to set a threshold. When the distance between the mouse and the target line segment is smaller than this threshold, the system determines that the line is picked up.



In the figure, click p (x, y) to determine whether the selected line (P1, P2) is the distance from P to the straight line (P1, P2. If the linear equation AX + by + c = 0 is known, the distance from the point (x, y) to the straight line is d = FABS (AX + by + C) /SQRT (A * A + B * B ).

Known on the line of two points (P1, P2), then the linear equation is (y1-y2) * x + (x2-x1) * Y + X1 * y2-x2 * Y1 = 0.

This is the general practice, but the complicated formula makes our program not more variables, but complicated statements that are hard to understand.

I got the idea from an online article "how to pick up and Judge straight lines in a drawing program" (there are some problems with the program in "how to pick up and Judge straight lines in a drawing program, corrected in this article ).


Let's move the above line segments and points in the coordinate system. The complicated problem will be much simpler, that is, moving the coordinate origin point to the end point of the straight line (X2, Y2), for example:



That is, before calculation, perform the following Transformation:

X1-= x2;

Y1-= Y2;

X-= x2;

Y-= Y2;

X2 = 0;

Y2 = 0;

In this way, the linear equation becomes y1X-x1Y = 0, and the formula for distance from point to line is also changed to d = FABS (Y1 * x-x1 * Y)/SQRT (Y1 * Y1 + X1 * X1 ), is it much simpler?

Finished? No! If this determination is complete, click the extension line of the online segment to determine the selected one! Therefore, if you do not click a rect that contains a line segment, the collection is invalid. The specific implementation code is as follows:


/*! <Br/> * @ brief determines whether a line segment is selected when you click the current mouse. <br/> * @ Param [in] P: Click the coordinate of the current mouse. <br/> * @ Param [in] coordinates of one endpoint of a P1 straight line <br/> * @ Param [in] coordinates of another endpoint of a P2 straight line <br/> * @ Param [in] the threshold value selected by dblimit <br/> * @ return indicates whether or not it is selected. "True" indicates that the image is selected, and "false" indicates that the image is not selected. <Br/> */<br/> bool isselline (cpoint P, cpoint P1, cpoint P2, double dblimit) <br/> {<br/>/* determines whether the current clicked point is in a straight line */<br/> int nradius = static_cast <int> (dblimit ); <br/> int L = (p1.x <= p2.x? P1.x: p2.x); <br/> int t = (p1.y <= p2.y? P1.y: p2.y); <br/> int r = (p1.x> p2.x? P1.x: p2.x); <br/> int B = (p1.y> p2.y? P1.y: p2.y); <br/> crect RT (L-nradius, T-nradius, R + nradius, B + nradius); <br/> If (! Rt. ptinrect (p) <br/> return false; <br/> // coordinate transformation, that is, to translate the CIDR block to the origin (p2.x, p2.y) <br/> p1.x-= p2.x; <br/> p1.y-= p2.y; <br/> P. x-= p2.x; <br/> P. y-= p2.y; <br/> p2.x = 0; <br/> p2.y = 0; <br/> // calculate the distance from a point to a line segment <br/> double D = FABS (static_cast <double> (p1.y * P. x-p1.x * P. y)/SQRT (static_cast <double> (p1.y * p1.y + p1.x * p1.x); <br/> If (d <dblimit) <br/>{< br/> return true; <br/>}< br/> return false; <br/>}



The crect and cpoint classes in MFC are used above. The interfaces of these two classes are simple. If you do not want to borrow the interfaces of MFC, you can simply implement the above functions.


References:


1. How to pick and judge a straight line in the drawing program
<G id = "1">, </G>, the implementation of this article is not my own discovery. I am very grateful to the author of "linear pick-up judgment in the drawing program .)









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.