polygons_intersection2d/
utils2d.rs

1use core::f32::consts::{FRAC_PI_2, FRAC_PI_4};
2use std::sync::Arc;
3
4use kiss3d::prelude::*;
5use parry2d::shape::TriMesh;
6
7/// As this file is used as a module from other examples,
8/// rustc warns about dead code:
9/// - `main()` is needed for this file to be included in examples
10/// - For other functions, they may be "dead code" for an example, but not for others.
11#[allow(dead_code)]
12fn main() {
13    println!(
14        "This module contains helper functions to use kiss3d,
15    isolated from the rest of the examples for the sake of simplicity."
16    );
17}
18
19/// Uses [`kiss3d`] to display the line passed as parameter.
20#[allow(dead_code)]
21pub fn draw_polyline(window: &mut Window, polyline: Vec<(Vec2, Vec2)>, color: Color) {
22    for line in polyline {
23        let a = line.0;
24        let b = line.1;
25        draw_line_2d(window, a, b, color);
26    }
27}
28
29/// Draws a text in the top left corner of the screen.
30///
31/// This uses a hardcoded position, size, color.
32#[allow(dead_code)]
33pub fn draw_text(window: &mut Window, font: &Arc<Font>, text: &str) {
34    window.draw_text(text, Vec2::new(10.0, 66.0), 30.0, font, WHITE);
35}
36
37/// Returns [lissajous curve](https://en.wikipedia.org/wiki/Lissajous_curve) coordinates for time `t`.
38///
39/// This uses hardcoded parameters to have an arbitrary pleasing trajectory.
40#[allow(dead_code)]
41pub fn lissajous_2d(t: f32) -> Vec2 {
42    // Some hardcoded parameters to have a pleasing lissajous trajectory.
43    lissajous_2d_with_params(t, 3.0, 2.0, FRAC_PI_2, FRAC_PI_4)
44}
45
46/// Returns [lissajous curve](https://en.wikipedia.org/wiki/Lissajous_curve) coordinates.
47#[allow(dead_code)]
48pub fn lissajous_2d_with_params(t: f32, a: f32, b: f32, delta_x: f32, delta_y: f32) -> Vec2 {
49    // Some hardcoded parameters to have a pleasing lissajous trajectory.
50
51    let x = (a * t + delta_x).sin();
52    let y = (b * t + delta_y).sin();
53    Vec2::new(x, y) * 0.75f32
54}
55
56/// Uses [`kiss3d`] to display the line passed as parameter.
57#[allow(dead_code)]
58pub fn draw_line_2d(window: &mut Window, a: Vec2, b: Vec2, color: Color) {
59    window.draw_line_2d(a, b, color, 2.0);
60}
61
62/// Uses [`kiss3d`] to display the trimesh passed as parameter.
63#[allow(dead_code)]
64pub fn draw_trimesh2(window: &mut Window, trimesh: &TriMesh, offset: Vec2) {
65    let vertices = trimesh.vertices();
66    for v in trimesh.indices() {
67        let v0 = vertices[v[0] as usize] + offset;
68        let v1 = vertices[v[1] as usize] + offset;
69        let v2 = vertices[v[2] as usize] + offset;
70
71        window.draw_line_2d(v0, v1, WHITE, 2.0);
72        window.draw_line_2d(v0, v2, WHITE, 2.0);
73        window.draw_line_2d(v2, v1, WHITE, 2.0);
74    }
75}
76
77/// Uses [`kiss3d`] to display a wireframe of the polygon.
78#[allow(dead_code)]
79pub fn draw_polygon(window: &mut Window, polygon: &[Vec2], scale: f32, shift: Vec2, color: Color) {
80    for i in 0..polygon.len() {
81        let a = polygon[i];
82        let b = polygon[(i + 1) % polygon.len()];
83        window.draw_line_2d(a * scale + shift, b * scale + shift, color, 2.0);
84    }
85}
86
87/// Uses [`kiss3d`] to display a cross, representing a point.
88#[allow(dead_code)]
89pub fn draw_point(window: &mut Window, point: Vec2, scale: f32, shift: Vec2, color: Color) {
90    let edge_len = 0.15;
91    let scaled_point = point * scale + shift;
92    let edge_offset = Vec2::new(edge_len * scale, 0.0);
93    let vert_offset = Vec2::new(0.0, edge_len * scale);
94    window.draw_line_2d(
95        scaled_point - edge_offset,
96        scaled_point + edge_offset,
97        color,
98        2.0,
99    );
100    window.draw_line_2d(
101        scaled_point - vert_offset,
102        scaled_point + vert_offset,
103        color,
104        2.0,
105    );
106}
107
108/// Draws a circle outline.
109#[allow(dead_code)]
110pub fn draw_circle(window: &mut Window, center: Vec2, radius: f32, color: Color) {
111    let segments = 32;
112    let tau = std::f32::consts::TAU;
113
114    for i in 0..segments {
115        let angle1 = (i as f32 / segments as f32) * tau;
116        let angle2 = ((i + 1) as f32 / segments as f32) * tau;
117
118        let p1 = center + Vec2::new(radius * angle1.cos(), radius * angle1.sin());
119        let p2 = center + Vec2::new(radius * angle2.cos(), radius * angle2.sin());
120        window.draw_line_2d(p1, p2, color, 2.0);
121    }
122}
123
124/// Draws a 2D AABB (axis-aligned bounding box) as a rectangle outline.
125#[allow(dead_code)]
126pub fn draw_aabb2(window: &mut Window, mins: Vec2, maxs: Vec2, color: Color) {
127    window.draw_line_2d(
128        Vec2::new(mins.x, mins.y),
129        Vec2::new(maxs.x, mins.y),
130        color,
131        2.0,
132    );
133    window.draw_line_2d(
134        Vec2::new(maxs.x, mins.y),
135        Vec2::new(maxs.x, maxs.y),
136        color,
137        2.0,
138    );
139    window.draw_line_2d(
140        Vec2::new(maxs.x, maxs.y),
141        Vec2::new(mins.x, maxs.y),
142        color,
143        2.0,
144    );
145    window.draw_line_2d(
146        Vec2::new(mins.x, maxs.y),
147        Vec2::new(mins.x, mins.y),
148        color,
149        2.0,
150    );
151}