TOGO - Basic 2D geometric operations
This library provides 2D geometric operations for arcs and line segments. It is used in my other projects and may not implement all possible geometric operations.
Adding the library to Cargo.toml
[]
= "0.4.1"


Documentation
Implemented Features
- Point creation and manipulation
- Line segments and circle arcs
- Distance calculations between points, line segments, and circle arcs
- Intersection tests for various geometric primitives
- Arc representation and manipulation
- Support for polylines and vertex manipulation
Distance Functions
- dist_arc_arc
- dist_line_circle
- dist_point_arc
- dist_point_circle
- dist_point_segment
- dist_segment_arc
- dist_segment_circle
- dist_segment_segment
Intersection Functions
- int_arc_arc
- int_circle_circle
- int_interval_interval
- int_line_arc
- int_line_circle
- int_line_line
- int_segment_arc
- int_segment_circle
- int_segment_segment
- if_really_intersecting_arc_arc
- if_really_intersecting_segment_arc
- if_really_intersecting_segment_segment
Geometric Primitives
- Points
- Line Segments
- Circles
- Circle Arcs
- Polylines
- Intervals
- PVertices (point, bulge)
Point (vector) Manipulation
- add, sub, neg, mul(f64), div(f64)
- dot
- perp
- norm
- normalize
- almost_eq (ULP-s)
- close_enough (eps)
- lerp
- sort_colinear_points
Utilities functions
- almost_equal_as_int (ULP-s)
- perturbed_ulps_as_int (ULP-s)
- close_enough (eps)
- diff_of_prod
- sum_of_prod
Algorithms
- Convex Hull (Pointline)
- Convexity Detection (Pointline)
- Area Calculations (Pointline and Arcline)
- Bounding Circle (Arc)
- Bounding Rectangle (Arc)
Examples
Creating and working with points (vectors)
use *;
// Create points using the constructor or convenience function
let p1 = new;
let p2 = point;
// Points support arithmetic operations
let sum = p1 + p2;
assert_eq!;
assert_eq!;
// Calculate distance between points
let distance = .norm;
assert!;
Working with geometric primitives
use *;
// Create a circle and segment
let center = point;
let c = circle;
let seg = segment;
assert_eq!; // Circle center field is 'c'
assert_eq!; // Circle radius field is 'r'
assert_eq!;
assert_eq!;
Distance computations
use *;
// Distance from point to circle returns (distance, closest_point, is_equidistant)
let p = point;
let c = circle;
let = dist_point_circle;
assert_eq!;
// Distance from point to segment returns (distance, closest_point)
let seg = segment;
let p = point;
let = dist_point_segment;
assert_eq!;
Intersection computations
use *;
// Test intersection between two circles
let c1 = circle;
let c2 = circle;
let result = int_circle_circle;
// Two circles with overlapping areas should intersect at two points
match result
Working with arcs
[!IMPORTANT] Arcs are always CCW (counter-clockwise) in this library.
use *;
// Create an arc from three points and radius (start, end, center, radius)
let start = point;
let end = point;
let center = point;
let a = arc;
assert_eq!; // Arc start point field is 'a'
assert_eq!; // Arc end point field is 'b'
assert_eq!; // Arc center field is 'c'
assert_eq!; // Arc radius field is 'r'
Working with lines
use *;
// Create a line from a point and direction vector
let origin = point;
let direction = point;
let l = line;
assert_eq!;
assert_eq!;
Working with intervals
use *;
// Create an interval (tuple struct with two f64 values)
let iv = interval;
assert_eq!; // First endpoint
assert_eq!; // Second endpoint
// Test if a value is contained in the interval
assert!;
assert!;
Working with polylines (PVertex)
use *;
// Create vertices for a polyline
let p1 = pvertex;
let p2 = pvertex;
let p3 = pvertex;
let polyline = vec!;
// Translate the polyline (returns a new polyline)
let offset = point;
let translated = polyline_translate;
assert_eq!;
assert_eq!;
Arc-arc distance computation
use *;
// Create two separate arcs
let a1 = arc;
let a2 = arc;
// Compute distance between arcs (returns just the distance as f64)
let dist = dist_arc_arc;
assert_eq!; // Distance between the arc edges
Line-circle intersection
use *;
// Create a line and circle that intersect
let l = line; // Horizontal line through origin
let c = circle;
let result = int_line_circle;
match result
Segment-segment intersection
use *;
// Create two intersecting segments
let seg1 = segment;
let seg2 = segment;
let result = int_segment_segment;
match result
Utility functions
use *;
// Test floating point equality with tolerance
assert!;
assert!;
// Check if two floats are almost equal using integer comparison
assert!;
Arc-Arc intersection
use *;
// Create two intersecting arcs
let a1 = arc;
let a2 = arc;
let result = int_arc_arc;
match result
Distance computations
use *;
let l = line; // Line with point and direction
let c = circle;
let result = dist_line_circle;
match result
// Distance from point to arc
let p = point;
let a = arc;
match dist_point_arc
// Distance from segment to arc
let seg = segment;
let a = arc;
let dist = dist_segment_arc;
assert_eq!;
use *;
// Distance from segment to circle
let seg = segment;
let c = circle;
let result = dist_segment_circle;
// Function returns DistSegmentCircleConfig enum
match result
// Distance between two segments
let seg1 = segment;
let seg2 = segment;
let dist = dist_segment_segment;
assert_eq!; // Parallel segments 2 units apart
Intersection computations
use *;
let seg1 = segment;
let seg2 = segment;
let dist = dist_segment_segment;
assert_eq!; // Parallel segments 2 units apart
Intersection computations
use *;
// Interval-interval intersection
let iv1 = interval;
let iv2 = interval;
let result = int_interval_interval;
match result
// Line-line intersection
let l1 = line; // Line with origin and direction
let l2 = line; // Line with origin and direction
let result = int_line_line;
match result
Area Calculations
use *;
use ;
// Calculate area of a polygon defined by points
let triangle = vec!;
let area = pointline_area;
assert_eq!; // Triangle area = 0.5 * base * height = 0.5 * 4 * 3 = 6
// Calculate area of a shape with both line segments and arcs
let square_with_arc = vec!;
let area_with_arc = arcline_area;
// Area includes the square plus the semicircular bulge
assert_eq!;
Bounding Calculations
use *;
use ;
// Find the smallest circle that contains an arc
let quarter_arc = arc;
let bounding_circle = arc_bounding_circle;
// For a quarter circle, the bounding circle is smaller than the arc's own circle
assert_eq!; // sqrt(2)/2
// Find the smallest axis-aligned rectangle that contains an arc
let semicircle = arc;
let bounding_rect = arc_bounding_rect;
// Rectangle should span from -1 to 1 in x, and include the arc's extremes
assert_eq!; // min_x
assert_eq!; // max_x
// Bounding rectangle for a line segment
let line_segment = arcseg;
let line_rect = arc_bounding_rect;
assert_eq!; // Bottom-left corner
assert_eq!; // Top-right corner
Convex Hull Computation
use *;
use ;
// Find the convex hull of a set of points
let points = vec!;
let hull = pointline_convex_hull;
// Hull should contain only the exterior points
assert_eq!; // 4 points on the convex hull
// Check if a polygon is convex
let convex_polygon = vec!;
assert!; // Square is convex
let concave_polygon = vec!;
assert!; // This shape is concave