Algorithm Series (ix) several commonly used algorithms for computing geometry and graphics (i)

Source: Internet
Author: User
Tags bool

My major is computer-aided design (CAD), half of the machine half of the software, "computer graphics" is a required course, but also my favorite course. I'm passionate about using code to fix everything. Almost every algorithm in this textbook has been achieved, this duplication of work, although the significance is not significant, but a lot of harvest, especially discarded for many years of math and back to the head, is the biggest harvest it. Although I have graduated for many years, but every time I review the code of these algorithms, I feel very surging, if I replaced the present, I am afraid no longer have the power to do these things.

In the study of "computer graphics" before, always feel a lot of things inscrutable, but the actual grasp, but found that there is no mystery, as the original portrait of the same God as the worship of the fire by the modern people diao in the mouth playing the same feeling. One of the foundations of graphics is computational geometry, but there is no theoretical mathematics so inscrutable, it is very practical, sometimes even simple to inconceivable. Computational geometry is a new subject born with the application of computer and CAD, which is called "Computer Aided geometric design (Computer aided geometric Design,cagd)" in foreign countries. The next few articles in the "algorithm series" will introduce some of the graphics of the common computational geometry algorithms (by the way to bask in my old code), are some of the basic graphics algorithms, need some graphics knowledge and mathematical knowledge, but it is not difficult. Don't believe it? Let's see how hard it is.

This article is the first, mainly some graphics commonly used computational geometry methods, involving vectors, point and line relations and point and polygon relations to solve mathematical knowledge, there are some basic principles of plane geometry. In advance, it is stated that the algorithm implemented in this paper is aimed at explaining the principle and revealing the essence of the algorithm. The efficiency and readability of the two considerations, more attention to readability, and sometimes in order to improve readability will deliberately take the "inefficient" code form, the actual project used in the code is certainly more compact and efficient, But the algorithm principle is the same, ask readers to have a correct understanding of this.

First, judge whether the point is within the rectangle

What is the relationship between computer graphics and mathematics? Let's take a look at a few examples to add some perceptual knowledge. The first is to determine whether a point is in the rectangle of the algorithm, this is a very simple algorithm, but very important. For example, if you click on a button, how does the system know that you want to trigger the corresponding event instead of another button? By the way, it's just a point of whether the judgment in the rectangle is handled. Windows API provides the PtInRect () function, the realization method is to determine whether the point of the X and Y coordinates at the same time in the rectangle's x-coordinate and y-coordinate range, the implementation of the algorithm is also very simple:

ispointinrect bool

(const rect& RC, const point& p)   
       
151 {   
       
152     Double XR = (p.x-rc.p1.x) * (P . x-rc.p2.x);   
       
153     Double yr = (p.y-rc.p1.y) * (P.Y-RC.P2.Y);   
       
154    
       
(     XR <= 0.0) && (yr <=));   
       
156}

See if the implementation of the Ispointinrect () function is different from what you think it is? Sometimes the hardware implementation multiplication has difficulty or is limited to the CPU multiplication instruction efficiency, may consider uses the following function substitution, the code is cumbersome, but avoids the multiplication operation:

 bool Ispointinrect (const rect& RC, const point& p) 121 {122 Double xl,xr,yt,y   
       
b   
       
123 124 if (rc.p1.x < rc.p2.x) is {126 XL = rc.p1.x;   
       
127 XR = rc.p2.x;   
       
128} 129 else 130 {131 XL = rc.p2.x;   
       
132 XR = rc.p1.x; 134 135 if (Rc.p1.y < rc.p2.y) 136 {137 YT = RC.   
       
P2.Y;   
       
138 YB = RC.P1.Y;   
       
139} 140 else the rc.p1.y;   
       
143 YB = Rc.p2.y; 144} 145 146 return (p.x >= XL && p.x <= XR) && (p.y >= YB &A   
       
mp;& p.y <= yt)); 147} 

Since the Ispointinrect () function does not assume that the two points of the rectangle are sorted in ascending order by axis, all possible coordinate ranges are considered when the algorithm is implemented. The Ispointinrect () function uses a planar rectangular coordinate system, and if it is not specifically stated, all of the algorithms in this paper are designed based on a planar rectangular coordinate system. In addition, the Ispointinrect () function does not specify a specific floating-point precision range, the default is the maximum precision of the system floating-point number, only in some cases must be compared with 0, the use of 10-8-point precision, if no special instructions, all of the algorithms in this article are handled in this way.

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.