Nyis OJ 683-point order (computational geometry basis)

Source: Internet
Author: User

Three-point sequential time limit: +Ms | Memory Limit:65535KB Difficulty:3
Descriptive narrative

Now give you the coordinates of the three point a,b,c, they must be able to form a triangle, and now let you infer whether a,b,c is given clockwise or counterclockwise?

Such as:

Figure 1: Clockwise given

Figure 2: Counter-clockwise

 

< figure 1> < figure 2>

Input
Each row is a set of test data, with 6 integer x1,y1,x2,y2,x3,y3 representing the horizontal ordinate of a,b,c three points, respectively. (Coordinate values from 0 to 10000)
Input 0 0 0 0 0 0 indicates end of input
Test data no more than 10000 groups
Output
Let's say these three points are given clockwise. Please output 1. Output 0 Counterclockwise
Example input
0 0 1 1 1 30 1 1 0 0 00 0 0 0 0 0
Example output
01
Source

Source=%e8%ae%a1%e7%ae%97%e5%87%a0%e4%bd%95%e5%9f%ba%e7%a1%80 "style=" text-decoration:none; Color:rgb (55,119,188) "> Computational Geometry Fundamentals

uploaded by

Zhang Yunzun

When you start to do this problem, you always draw on the draft paper. Infer how many cases, just think that the situation is more, may not consider the overall, beginning I was based on a point as a vertex, and then write the criteria. Later, I wrote it myself and executed it, and the results were all 1. I think I have repeated the situation, my ideas may be biased, including other situations.

if ((Y1>=Y2&&Y1>=Y3&&X3<=X2)//with a as a vertex           | | (y2>=y1&&y2>=y3&&x3>=x1)//b for vertex           | | (Y3>=Y2&&Y3>=Y1&&X1<=X2)) C is the vertex            printf ("1\n");       else if ((y1>=y2&&y1>=y3&&x2<=x3)          | | | (y2>=y1&&y2>=y3&&x3<=x1)          | | (y3>=y2&&y3>=y1&&x2<=x1))          printf ("0\n");

Later a look at the discussion area, actually has the formula ...

No sense in calculating geometry.

The formulas that have been learned are not used. Take advantage of the cross product we have learned.

the use of vector cross-product inference is counterclockwise or clockwise. With vector P = (x1, y1), Q = (x2, y2), the vector fork product is defined as a signed area of 0,0 consisting of (P1), p2, p1+p2, and parallelogram, i.e. PXQ = x1*y2-x2*y1. The result is a scalar. Obviously there are properties Pxq =-(QXP) and PX (-Q) =-(PXQ).
a very important property of the cross product is the ability to infer a clockwise-counterclockwise relationship between two vectors through its notation:

if PXQ > 0, then P is in the clockwise direction of Q.
if Pxq < 0, then P is in the counterclockwise direction of Q.
if Pxq = 0, P is collinear with Q, but may also be reversed.



explanation:
axb= (AY * bz-by * az, AZ * bx-a X * BZ, AX * by-ay * bx) and since AZ Bz are all 0, so axb= (0,0. AX * By-ay * bx)
According to the right hand system (fork to meet the right hand line), if PXQ > 0,ax * by-ay * bx>0, that is, thumb pointing upward, so P in the clockwise direction of Q. A bit.

Read someone else's blog about the content of the cross-product http://blog.csdn.net/sjl_leaf/article/details/8789785 immediately on the cottage open Ah, more than 10 lines of code to conquer;

#include <cstdio>int main () {   int x1,y1,x2,y2,x3,y3;   int flag;   while (scanf ("%d%d%d%d%d%d", &x1,&y1,&x2,&y2,&x3,&y3))   {       if (!x1&&!y1 &&!X2&&!Y2&&!X3&&!Y3) break        ;      flag= (x2-x1) * (y3-y1)-(y2-y1) * (X3-X1);      if (flag<0) printf ("1\n");      else printf ("0\n");   }    return 0;}
The three points are converted into vectors for calculation, ab= (x2-x1,y2-y1), ac= (x3-x1,y3-y1);

Their cross-product is |x2-x1,y2-y1|.

|x3-x1,y3-y1|

Use determinant calculations. able to draw flag= (x2-x1) * (y3-y1)-(y2-y1) * (X2-X1);

Value of inferred flag

1.flag<0 Clockwise

2.flag>0 Counter-clockwise

3.flag=0 three-point common line

Nyis OJ 683-point order (computational geometry basis)

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.