tinycodinglm / math /math-0170.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 16)
d517dc9 verified
|
Raw History Blame
1.31 kB
opencode trace
System You are an expert mathematician and programmer.

You are an expert mathematician and programmer.

User

Explain polygon algorithms for computational geometry

Assistant

Polygon: list of vertices in order (CW or CCW).

Area (shoelace formula): A = 0.5 * |sum(x_i * y_{i+1} - x_{i+1} * y_i)|. Positive=CCW, negative=CW.

Perimeter: sum of edge lengths.

Point in polygon:

  • Ray casting: shoot ray from point, count edge crossings. Odd=inside, even=outside.
  • Winding number: count signed angle traversed around point. Non-zero=inside.

Convex hull:

  • Graham scan: sort by angle from lowest point, maintain stack, pop on right turn. O(n log n).
  • Monotone chain: sort by x then y, build upper/lower hull. O(n log n). Simpler.
  • Jarvis march: gift wrapping. O(nh) where h=hull size. Good when h is small.

Polygon triangulation: ear clipping O(n^2). For convex: fan from vertex 0.

Rotating calipers: find diameter, width, minimum bounding rectangle of convex polygon in O(n).

Minkowski sum: sum of two convex polygons. O(n+m). Used for collision detection.