POJ1584: A Round Peg in a Ground Hole (Cross Product, convex hull)

Source: Internet
Author: User

A Round Peg in a Ground Hole
Time Limit: 1000 MS Memory Limit: 10000 K
Total Submissions: 4098 Accepted: 1246
Description

The DIY Furniture company specializes in assemble-it-yourself furniture kits. typically, the pieces of wood are attached to one another using a wooden peg that fits into pre-cut holes in each piece to be attached. the pegs have a circular cross-section and so are intended to fit inside a round hole.
A recent factory run of computer Compute S were flawed when an automatic grinding machine was mis-programmed. the result is an irregularly shaped hole in one piece that, instead of the expected circular shape, is actually an irregular polygon. you need to figure out whether the specified s need to be scrapped or if they can be salvaged by filling a part of the hole with a mixture wood of shavings and glue.
There are two concerns. first, if the hole contains any protrusions (I. e ., if there exist any two interior points in the hole that, if connected by a line segment, that segment wowould cross one or more edges of the hole ), then the filled-in-hole wocould not be structurally sound enough to support the peg under normal stress as the furniture is used. second, assuming the hole is appropriately shaped, it must be big enough to allow insertion of the peg. since the hole in this piece of wood must match up with a corresponding hole in other pieces, the precise location where the peg must fit is known.
Write a program to accept descriptions of pegs and polygonal holes and determine if the hole is ill-formed and, if not, whether the peg will fit at the desired location. each hole is described as a polygon with vertices (x1, y1), (x2, y2 ),..., (xn, yn ). the edges of the polygon are (xi, yi) to (xi + 1, yi + 1) for I = 1... n −1 and (xn, yn) to (x1, y1 ).
Input

Input consists of a series of piece descriptions. Each piece description consists of the following data:
Line 1 <nVertices> <pegRadius> <pegX> <pegY>
Number of vertices in polygon, n (integer)
Radius of peg (real)
X and Y position of peg (real)
N Lines <vertexX> <vertexY>
On a line for each vertex, listed in order, the X and Y position of vertex The end of input is indicated by a number of polygon vertices less than 3.
Output

For each piece description, print a single line containing the string:
Hole is ill-FORMED if the hole contains protrusions
Peg will fit if the hole contains no protrusions and the peg fits in the hole at the indicated position
Peg will not fit if the hole contains no protrusions but the peg will not fit in the hole at the indicated position
Sample Input

5 1.5 1.5 2.0
1.0 1.0
2.0 2.0
1.75 2.0
1.0 3.0
0.0 2.0
5 1.5 1.5 2.0
1.0 1.0
2.0 2.0
1.75 2.5
1.0 3.0
0.0 2.0
1
Sample Output

Hole is ill-FORMED
PEG WILL NOT FIT
Source

Mid-Atlantic 2003
MYCode:
# Include <iostream>
# Include <cstring>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Define inf 100000000
Using namespace std;
# Deprecision MAX 1010
Struct node
{
Double x, y;
} A [MAX];
Node tp [MAX];
Node center;
Double r;
Node p;
Double cw (node a, node B, node c)
{
Return (B. x-a.x) * (c. y-a.y)-(c. x-a.x) * (B. y-a.y );
}
Double dist (node a, node B)
{
Return sqrt (a. x-b.x) * (a. x-b.x) + (a. y-b.y) * (a. y-b.y ));
}
Double dist (node a, node B, node c)
{
Double d = dist (a, B );
Double res = cw (a, B, c );
Return fabs (res/d );
}
Bool cmp (node a, node B)
{
Double ans = cw (p, a, B );
If (ans! = 0) return cw (p, a, B)> 0;
Else return dist (p, a)> dist (p, B); // note
}
Int main ()
{
Int n;
While (scanf ("% d", & n )! = EOF)
{
If (n <3)
Break;
Scanf ("% lf", & r, & center. x, & center. y );
Int I;
For (I = 0; I <n; I ++)
{
Scanf ("% lf", & a [I]. x, & a [I]. y );
}
Bool flag = true;
Double dirt = 0;
For (I = 0; I <n-1; I ++)
{
If (cw (a [I], a [I + 1], a [(I + 2) % n])! = 0)
{
If (dirt = 0)
Dirt = cw (a [I], a [I + 1], a [(I + 2) % n]);
Else if (cw (a [I], a [I + 1], a [(I + 2) % n])> 0 & dirt <0
| Cw (a [I], a [I + 1], a [(I + 2) % n]) <0 & dirt> 0)
{
Flag = false;
Break;
}
}
}
If (flag = false)
{
Printf ("hole is ill-FORMED \ n ");
Continue;
}
For (I = 0; I <n; I ++)
{
Double d = dist (a [I], a [(I + 1) % n], center );
Double res = cw (a [I], a [(I + 1) % n], center );
If (d <r | res <0 & dirt> 0
| Res> 0 & dirt <0 | res = 0)
{
Flag = false;
Break;
}
}
If (! Flag)
{
Printf ("peg will not fit \ n ");
}
Else
Printf ("peg will fit \ n ");
}
}

//

First, you must determine whether the polygon is convex.

Because the question is to give all vertices clockwise or counterclockwise, you do not need to sort all vertices.

Use cross product to determine convex hull

Allowed multi-point collinearity

The trap is:

The center of the circle may not be inside the polygon.

The distance from the center to each side of the polygon is required. The distance between all sides must be greater than the radius.

Method for Finding the distance from a point to a line segment: Use cross product

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.