dymokomi/luced-3d
Source of release 0.11.3
0.11.3 / src / sketch_geometry.luc
## Plane geometry on a live sketch for the editor's tools: a curve's ends,
## which side of a line a place is, how far a place is from a curve's
## carrier. (What luce-cad computes, it keeps: picking, snapping, cuts.)
from luce_cad.sketch import CadSketch
from sketch_kinds import line_kind, circle_kind, arc_kind, spline_kind, conic_kind
from luce_std import math
## A curve's start point (-1 for a closed one: a circle, an ellipse).
pub func start_point(sketch: CadSketch, curve: int) -> int:
let k = sketch.kind(curve)
if k in [line_kind, spline_kind, conic_kind]:
return sketch.part(curve, 0)
return sketch.part(curve, 1) if k == arc_kind else -1
## A curve's end point (-1 for a closed one).
pub func end_point(sketch: CadSketch, curve: int) -> int:
let k = sketch.kind(curve)
if k == line_kind or k == conic_kind:
return sketch.part(curve, 1)
if k == spline_kind:
return sketch.part(curve, sketch.part_count(curve) - 1)
return sketch.part(curve, 2) if k == arc_kind else -1
## The line or arc point `point` is an end of, or -1.
pub func ended_curve(sketch: CadSketch, point: int) -> int:
if point < 0:
return -1
for at in 0..<sketch.entity_count():
if sketch.kind(at) in [line_kind, arc_kind] and (start_point(sketch, at) == point or end_point(sketch, at) == point):
return at
return -1
## Which side of line `line` (`x`, `y`) is: +1 its left, -1 its right (as
## Tangent keeps it).
pub func side_of(sketch: CadSketch, line: int, x: float, y: float) -> float:
let a = sketch.part(line, 0)
let b = sketch.part(line, 1)
let cross = (sketch.x(b) - sketch.x(a)) * (y - sketch.y(a)) - (sketch.y(b) - sketch.y(a)) * (x - sketch.x(a))
return 1.0 if cross >= 0.0 else -1.0
## Line `line`'s length.
pub func length_of(sketch: CadSketch, line: int) -> float:
let a = sketch.part(line, 0)
let b = sketch.part(line, 1)
return math.hypot(sketch.x(b) - sketch.x(a), sketch.y(b) - sketch.y(a))
## How far (`x`, `y`) is from curve `curve`'s carrier (a line's line, a
## circle's or arc's circle; a free-form curve itself).
pub func carrier_distance(sketch: CadSketch, curve: int, x: float, y: float) -> float:
let kind = sketch.kind(curve)
if kind == line_kind:
let a = sketch.part(curve, 0)
let b = sketch.part(curve, 1)
let dx = sketch.x(b) - sketch.x(a)
let dy = sketch.y(b) - sketch.y(a)
return math.abs(dx * (y - sketch.y(a)) - dy * (x - sketch.x(a))) / math.max(math.hypot(dx, dy), 1e-300)
if kind == circle_kind or kind == arc_kind:
let c = sketch.part(curve, 0)
return math.abs(math.hypot(x - sketch.x(c), y - sketch.y(c)) - sketch.radius(curve))
return sketch.distance(curve, x, y)
# -- Plane points as [x, y].
## The circle through three points, its center [x, y], or none (in a line).
pub func through(a: list[float], b: list[float], c: list[float]) -> list[float]?:
let d = 2.0 * (a[0] * (b[1] - c[1]) + b[0] * (c[1] - a[1]) + c[0] * (a[1] - b[1]))
if math.abs(d) <= 1e-12:
return none
let a2 = a[0] * a[0] + a[1] * a[1]
let b2 = b[0] * b[0] + b[1] * b[1]
let c2 = c[0] * c[0] + c[1] * c[1]
return [(a2 * (b[1] - c[1]) + b2 * (c[1] - a[1]) + c2 * (a[1] - b[1])) / d, (a2 * (c[0] - b[0]) + b2 * (a[0] - c[0]) + c2 * (b[0] - a[0])) / d]
## The counterclockwise turn from angle `a` to angle `b` (0..2π].
pub func turn(a: float, b: float) -> float:
var t = b - a
while t <= 0.0:
t += 2.0 * math.pi
while t > 2.0 * math.pi:
t -= 2.0 * math.pi
return t
## The counterclockwise turn about `o` from `a` to `b` (0..2π].
pub func turn_to(o: list[float], a: list[float], b: list[float]) -> float:
return turn(math.atan2(a[1] - o[1], a[0] - o[0]), math.atan2(b[1] - o[1], b[0] - o[0]))
## How far `p` is from the line through `a` and `b`.
pub func off_line(a: list[float], b: list[float], p: list[float]) -> float:
let length = math.max(math.hypot(b[0] - a[0], b[1] - a[1]), 1e-300)
return math.abs((b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0])) / length
## The square to side `a`–`b` toward `p`, and how far `p` is along it.
pub func across(a: list[float], b: list[float], p: list[float]) -> (float, float, float):
let l = math.max(math.hypot(b[0] - a[0], b[1] - a[1]), 1e-300)
let nx = -(b[1] - a[1]) / l
let ny = (b[0] - a[0]) / l
return (nx, ny, nx * (p[0] - a[0]) + ny * (p[1] - a[1]))