Determine if a bit is within the fan

Source: Internet
Author: User

1. Description of the problem:

Recently in a watch on the electronic pet project, the entire main function interface is a circle, and then each function (feeding, cleaning, pleasure, etc.) each is a part of the circle is a fan. Then click on each sector, the corresponding sub-functional interface, the format and the main function interface is consistent.

Now to achieve the click of the corresponding fan will appear corresponding sub-function interface, sub-functional interface and this similar. In other words, click on the interface, you need to determine the point of contact falling in which sector.

A simple idea at the time was to place a square button in the sector area and invoke the callback after clicking it, but the disadvantage was that it was not accurate enough because the button could not cover the whole pie, so this method is undesirable.

So you need to implement any area of the click, you have to be able to determine where the contact point belongs to.

2. Analysis:

Now the demand is clear, which is to determine which fan in the circle the contact point is inside. To judge whether a point is in the a0b sector.

By communicating with art, it is known that the two-point coordinate and the center coordinate of each sector in the circle. Suppose the center coordinates O (0, 0), medium A, a, b point coordinates a (x1, y1), (x2, y2), and any point coordinates p (x, y) that can be obtained when clicked.

Now discuss the situation (0 <= AOB <= 180) degrees.

The following three conditions are satisfied in the sector of the judging point:

1. Fan-shaped "start arm" to rotate counterclockwise through the P point.

2. Fan-shaped "end arm" to rotate clockwise through the P point.

3. Click inside the circle.

Such as:

  

Proof 1 "start arm" 0A vector to op vector is rotated clockwise:

1.) Do "start arm" OA vector "counterclockwise direction" to 90 degrees of normal vector (normal vector), the use of two vertical vectors with a point multiplication of 0 of the nature of the normal vector can be expressed as N1 (-y1, X1).

2.) N1 and OP two vectors do dot product N1*op = |n1|*| Op|*cosa (A is the angle between N1 and OP), if the result of the dot product is greater than 0, then 0 < A < 90, and OA and n1 angle = 90. You can then prove that the 0A vector to the OP vector is rotated clockwise.

Conversely, the dot product result is less than 0, which means that the 0A vector to the OP vector is rotated counterclockwise. here is the Encyclopedia of dot product: http://baike.baidu.com/view/2744555.htm

It can be understood that OA counter-clockwise rotation 90 degrees to get N1, if the angle of OP and N1 is less than 90 degrees, then OA counterclockwise to the op.

Such as

  

Next it turns out that "end arm" to OP is rotated clockwise:

1.) Do "end arm" ob vector "counterclockwise direction" to 90 degrees of normal vector (normal vector), the use of two vertical vectors with a point multiplication of 0 of the nature of the normal vector can be expressed as N2 (-y2, x2).

2.) N2 and OP two vectors do dot product N2*op = |n2|*| OP|*COSB (b is the angle between N1 and OP), if the result of the dot product is less than 0, then 0 < b < 90, and the angle of ob and N2 = 90. You can then prove that the 0B vector to the OP vector is rotated counterclockwise.

Conversely, the dot product result is greater than 0, indicating that the 0B vector to the OP vector is rotated clockwise.

Such as:

  

Here is a function that determines whether the fan-shaped "start arm" or "End Arm" to OP is clockwise or counterclockwise:

function Areclockwise (varm, Vop)

{

RETURN-VARM.Y * vop.x + varm.x * vop.y > 0;

}

Description: The vertical vector of the varm is represented as (-VARM.Y, varm.x), so the top is the dot product of two vectors.

Next determine whether the point P is within the circle, this is very simple

function Iswithinradius (V, radiussquared)

{
return v.x*v.x + v.y*v.y <= radiussquared;
}

So the three proofs above, the bottom is the overall code, of course, can be implemented in any language:

function Isinsidesector (point, Center, Sectorstart, Sectorend, radiussquared) {//point is arbitrary, Sectorstart is "start Arm ", the same sectorend  . var relpoint = {    - center.x,    - center.y  };   return !areclockwise (Sectorstart, relpoint) &&         &&          Iswithinradius (Relpoint, radiussquared);}

Note: The above Judgment fan Sectorstart and sectorend for the counter-clockwise order, and (0 <= angle AOB <= 180) degree of the case, if any point coincides with OA or OB coincident, you need to judge according to the specific needs of the sector.

  Original address: Http://stackoverflow.com/questions/13652518/efficiently-find-points-inside-a-circle-sector

--------------------------------------------------------------------------------------------------------------- -Split Line--------------------------------------------------------------------------------------------------------------------- -------

Another way of thinking:

As far as possible without the principle of trigonometric functions, it is also possible to compare the slope size to determine whether the point is within the sector. On the left side of the y-axis, the slope counter-clockwise direction is incremented, the y-axis to the right, the slope is also increment, using this property can be derived corresponding method.

Of course, it is also necessary to discuss the case where the sector contains the Y axis. Originally did not find the above method before, is to use this idea to achieve.

  

Extended:

1. How to use the above method to determine whether the given two "arm" around the fan angle is greater than 180?

1) First, do the reverse extension of OA, the intersection of the dot C, get the OC vector (-x1,-y1).

2) If the angle of OA and OB is greater than 180, then OC will be used counterclockwise rotation to the OB, then with the determination of the "condition 1" step calculation, if the resulting dot product greater than 0 means that the oa,ob of the sector is more than 180, and vice versa is less than 180.

2. When you do not know whether the oa,0b is more than 180, how to correctly determine whether the point P is in the fan?

1) First use the above method to determine whether the Oa,ob Circle fan >180. If it is greater than 180, then just determine if P-point is in the Ob,oa so-called fan (that is, star Tarm = OB, end arm = OA) can be.

The level is limited, inevitably error, welcome correction and pointing. I hope that by reading this article can provide readers with ideas to solve the problem.

  

Determine if a bit is within the fan

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.