You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: content/5_Plat/Geo_Pri.mdx
+76-4
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ You should know operations such as the cross product and dot product.
10
10
11
11
## Standard Problems
12
12
13
+
### Location of a point
13
14
<FocusProblemproblem="pointlocationtest" />
14
15
15
16
To check the $P$ location towards the $P_1$ $P_2$ line we use following formula: $(P.y - P_1.y) * (P_2.x - P_1.x) - (P.x - P_1.x) * (P_2.y - P_1.y)$
@@ -30,7 +31,7 @@ This formula doesn't only tell us whether the points are collinear, but where th
30
31
31
32
</Spoiler>
32
33
33
-
### Implementation
34
+
####Implementation
34
35
35
36
<LanguageSection>
36
37
<CPPSection>
@@ -78,12 +79,13 @@ int main() {
78
79
</CPPSection>
79
80
</LanguageSection>
80
81
82
+
### Segment intersection
81
83
<FocusProblem problem="line" />
82
84
83
85
We can quickly dismiss the segment intersection by treating them as rectangles having the segments as diagonals which can be easily done.
84
86
If it turns out the rectangles intersect then we just check if the segment's ends are on different sides of the other segment.
85
87
86
-
### Implementation
88
+
#### Implementation
87
89
88
90
<LanguageSection>
89
91
<CPPSection>
@@ -161,11 +163,12 @@ int main() {
161
163
</CPPSection>
162
164
</LanguageSection>
163
165
166
+
### Polygon area
164
167
<FocusProblem problem="polygon" />
165
168
166
169
The following algorithm uses the [Shoelace formula](https://en.wikipedia.org/wiki/Shoelace_formula).
167
170
168
-
### Implementation
171
+
#### Implementation
169
172
170
173
<LanguageSection>
171
174
<CPPSection>
@@ -205,6 +208,8 @@ int main() {
205
208
</CPPSection>
206
209
</LanguageSection>
207
210
211
+
### Point's location relative to polygon
212
+
208
213
<FocusProblem problem="polygon2" />
209
214
210
215
We can cast a ray from the point $P$ going in any fixed direction (people usually go to the right).
@@ -213,7 +218,7 @@ If the point is on the inside of the polygon then it will intersect the edge an
213
218
214
219
This approach is called [raycasting](https://en.wikipedia.org/wiki/Point_in_polygon).
215
220
216
-
### Implementation
221
+
####Implementation
217
222
218
223
<LanguageSection>
219
224
<CPPSection>
@@ -312,6 +317,73 @@ int main() {
312
317
</CPPSection>
313
318
</LanguageSection>
314
319
320
+
### Latticepointsinpolygon
321
+
<FocusProblemproblem="latticepoints" />
322
+
323
+
Let's first focus on the lattice points on the polygon'sboundary. We'll process each edge individually. The number of intersections of a line with lattice points is the greatest
324
+
commondivisorof$P_1.x - P_2.x$and$P_1.y - P_2.y$.
325
+
326
+
<Spoilertitle="Demonstration">
327
+
Usingthelinearfunctionitcanbeproven. Denotethesegmentendswith ($x_1$,$y_1$) and ($x_2$, $y_2$). Thefunctionhasthefollowing
328
+
form:&f(x) =ax+b&. Theslopeof$f(x)$, in our case $a$, is $\frac{y_1-y_2}{x_1-x_2}$, so $f(x)$ becomes $f(x) = \frac{y_1-y_2}{x_1-x_2} * x + b$.
329
+
330
+
This means that every points along the line has the following form: $(x, f(x))$whichhasbecome$(x, \frac{y_1-y_2}{x_1-x_2} *x+b)$. Number$x$can
331
+
beanyinteger, but &f(x)& no. In order to be lattice point &f(x)& has to be an integer, by eliminating the fraction resulting in $x$ divisible by $x_1 - x_2$.
332
+
333
+
</Spoiler>
334
+
335
+
Now that we know the number of lattice points on the boundary we can find the number of lattice points inside the polygon using [Pick's theorem](https://en.wikipedia.org/wiki/Pick%27s_theorem).
336
+
Let's denote $A$ polygon'sarea, $i$thenumberofintegerpointsinsideand$b$thenumberofintegerpointsonitsboundary. Then, accordingtoPick's theorem, we have the following
337
+
equation: $A=i+b/2- 1$. Changingtheorderalittlebittoget$i=A-b/2+ 1$. We've found $b$ and, as you'veprobablyalreadysolved [PolygonArea](https://cses.fi/problemset/task/2191) from above,
0 commit comments