-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
polygon: Move polygon rel. fn's to new file
- Loading branch information
1 parent
b69f81f
commit 6b9fa2b
Showing
4 changed files
with
72 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/// Returns a list of polygon points from | ||
/// a list of segments. | ||
/// | ||
/// Cubic segments get linearized by sampling. | ||
/// | ||
/// - segment (list): List of segments | ||
/// - samples (int): Number of samples | ||
/// -> List of vectors | ||
#let from-segments(segments, samples: 10) = { | ||
import "/src/bezier.typ": cubic-point | ||
let poly = () | ||
for ((kind, ..pts)) in segments { | ||
if kind == "cubic" { | ||
poly += range(0, samples).map(t => { | ||
cubic-point(..pts, t / (samples - 1)) | ||
}) | ||
} else { | ||
poly += pts | ||
} | ||
} | ||
return poly | ||
} | ||
|
||
/// Computes the signed area of a 2D polygon. | ||
/// | ||
/// The formula used is the following: | ||
/// $ 1/2 sum_i=0^n-1 x_i*y_i+1 - x_i+1*y_i $ | ||
/// | ||
/// - points (list): List of Vectors of dimension >= 2 | ||
/// -> Signed area | ||
#let signed-area(points) = { | ||
let a = 0 | ||
let n = points.len() | ||
let (cx, cy) = (0, 0) | ||
for i in range(0, n) { | ||
let (x0, y0, ..) = points.at(i) | ||
let (x1, y1, ..) = points.at(calc.rem(i + 1, n)) | ||
cx += (x0 + x1) * (x0 * y1 - x1 * y0) | ||
cy += (y0 + y1) * (x0 * y1 - x1 * y0) | ||
a += x0 * y1 - x1 * y0 | ||
} | ||
return .5 * a | ||
} | ||
|
||
/// Returns the winding order of a 2D polygon | ||
/// by using it's signed area. | ||
/// | ||
/// - point (list): List of polygon points | ||
/// -> "ccw" (counter clock-wise) or "cw" (clock-wise) or none | ||
#let winding-order(points) = { | ||
let area = signed-area(points) | ||
if area > 0 { | ||
"cw" | ||
} else if area < 0 { | ||
"ccw" | ||
} else { | ||
none | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters