Zoj1081 Problem Solving report

Source: Internet
Author: User

The general meaning of the question: Create a polygon for several points, and then give some points to determine whether the points are inside or outside the polygon.

A template question... I can't do mathematics. I can't write it myself. I understand the general idea. I searched the internet. There are many methods, one of which is to make the rays parallel to the X axis, then, determine the number of intersections. The odd number is inside and the even number is outside. note that the X-ray does not count when it passes through the vertices of a polygon. ah, it seems that the mathematics is too weak. the Code is as follows:

# Include <stdio. h>
# Include <math. h>
# Define max (A, B) (a)> (B )? (A) (B ))
# Define min (A, B) (a) <(B )? (A) (B ))
# Define infinity 1e9
# Define ESP 1e-7
# Define max_n 100
Struct point
{
Double X, Y;
};
Struct linesegment
{
Point pt1, pt2;
};
// Calculate the cross multiplication | p1p0 | × | p2p0 |
Double multiply (point P1, point P2, point P0)
{
Return (p1.x-Snapshot X) * (p2.y-Snapshot y)-(p2.x-Snapshot X) * (p1.y-Snapshot y ));
}

Bool isonline (point, linesegment line)
{
Return (FABS (multiply (line. pt1, line. pt2, point) <ESP )&&
(Point. x-line.pt1.x) * (point. x-line.pt2.x) <= 0 )&&
(Point. y-line.pt1.y) * (point. y-line.pt2.y) <= 0 ));
}
// Determine the intersection of line segments
Bool intersect (linesegment L1, linesegment l2)
{
Return (max (l1.pt1. X, l1.pt2. X)> = min (l2.pt1. X, l2.pt2. X ))&&
(Max (l2.pt1. X, l2.pt2. X)> = min (l1.pt1. X, l1.pt2. X ))&&
(Max (l1.pt1. Y, l1.pt2. Y)> = min (l2.pt1. Y, l2.pt2. y ))&&
(Max (l2.pt1. Y, l2.pt2. Y)> = min (l1.pt1. Y, l1.pt2. y)
(Multiply (l2.pt1, l1.pt2, l1.pt1) * multiply (l1.pt2, l2.pt2, l1.pt1)> = 0 )&&
(Multiply (l1.pt1, l2.pt2, l2.pt1) * multiply (l2.pt2, l1.pt2, l2.pt1)> = 0 ));
}

// Points in the Polygon
Int inpolygon (point polygon [], int N, point)
{
If (n = 1)
Return (FABS (polygon [0]. x-point.x) <ESP) & (FABS (polygon [0]. y-point.y) <ESP ));
Else if (n = 2)
{
Linesegment side;
Side. pt1 = polygon [0];
Side. pt2 = polygon [1];
Return isonline (point, side );
}
Int COUNT = 0, I;
Linesegment line;
Line. pt1 = point;
Line. pt2.y = point. Y;
Line. pt2.x =-infinity;

For (I = 0; I <n; I ++)
{
Linesegment side;
Side. pt1 = polygon [I];
Side. pt2 = polygon [(I + 1) % N];

If (isonline (point, side ))
Return true;

// If the side parallel X axis is not considered
If (FABS (side. pt1.y-side. pt2.y) <ESP)
Continue;
// 4. An endpoint is on a straight line
If (isonline (side. pt1, line ))
{
If (side. pt1.y> side. pt2.y)
Count ++;
}
Else if (isonline (side. pt2, line ))
{
If (side. pt2.y> side. pt1.y)
Count ++;
}
// Intersection
Else if (intersect (line, side ))
Count ++;
}

Return count % 2;
}

Int main ()
{
// Freopen ("in.txt", "r", stdin );
Int n, m, casenum = 0, I;
Point polygon [max_n];
Point P;
While (scanf ("% d", & N)
{
Casenum ++;
If (casenum> 1)
Printf ("\ n ");
Printf ("problem % d: \ n", casenum );
Scanf ("% d", & M );
For (I = 0; I <n; I ++)
Scanf ("% lf", & polygon [I]. X, & polygon [I]. y );

For (I = 0; I <m; I ++)
{
Scanf ("% lf", & P. X, & P. y );
If (inpolygon (polygon, n, p ))
Printf ("within \ n ");
Else
Printf ("outside \ n ");
}
}
Return 0;
}

Zoj1081 Problem Solving report

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.