From 9f5775a99dba3c2e3e9b5e5c6245724a07ab8b00 Mon Sep 17 00:00:00 2001 From: Lily Iliana Luna Ylva Anderson Grigaitis Date: Wed, 20 Aug 2025 20:48:21 -0500 Subject: [PATCH] Created a function for the conversion of coordinates from algebra space to screen space. Typo. --- src/cga.rs | 2 +- src/lib.rs | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/cga.rs b/src/cga.rs index 1d07707..1b74075 100644 --- a/src/cga.rs +++ b/src/cga.rs @@ -1,5 +1,5 @@ //Licensed from ganja.js (https://github.com/enkimute/ganja.js) under the MIT License -//Origin at: +//Original at: //https://github.com/enkimute/ganja.js/blob/6e97cb45d780cd7c661d9240f1610eb005707417/codegen/rust/cga.rs // 3D Projective Geometric Algebra diff --git a/src/lib.rs b/src/lib.rs index 07297b5..2d69bd5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,10 +84,15 @@ fn point_to_cga(x: f64, y: f64) -> CGA { 0.5*mag_sqr*n_vec + ux*x_vec + uy*y_vec - 0.5*n_bar } +fn point_to_screen_space(x: f64, y: f64) -> (f64, f64) { + let screen_x = x*RADIUS + CENTER_X; + let screen_y = y*RADIUS + CENTER_Y; + (screen_x, screen_y) +} + fn draw_point(context: &CanvasRenderingContext2d, point: &CGA) { //Naively assume we are given a valid point - let x = point[1]*RADIUS + CENTER_X; - let y = point[2]*RADIUS + CENTER_Y; + let (x,y) = point_to_screen_space(point[E1], point[E2]); context.begin_path(); context.set_fill_style_str(RED); @@ -125,8 +130,10 @@ fn draw_line(context: &CanvasRenderingContext2d, line: &CGA) { let p1y = py + ty * 1000.; let p2x = px - tx * 1000.; let p2y = py - ty * 1000.; - context.move_to(p1x*RADIUS + CENTER_X, p1y*RADIUS + CENTER_Y); - context.line_to(p2x*RADIUS + CENTER_X, p2y*RADIUS + CENTER_Y); + let (canvas_x1, canvas_y1) = point_to_screen_space(p1x, p1y); + let (canvas_x2, canvas_y2) = point_to_screen_space(p2x, p2y); + context.move_to(canvas_x1, canvas_y1); + context.line_to(canvas_x2, canvas_y2); } } else { @@ -139,12 +146,11 @@ fn draw_line(context: &CanvasRenderingContext2d, line: &CGA) { //draw_point(context, &point_to_cga(c_x, c_y)); //Convert into Canvas Space - let canvas_cx = c_x*RADIUS + CENTER_X; - let canvas_cy = c_y*RADIUS + CENTER_Y; + let (canvas_x, canvas_y) = point_to_screen_space(c_x, c_y); let canvas_r = r*RADIUS; //Draw - context.arc(canvas_cx, canvas_cy, canvas_r, 0., TWO_PI).unwrap(); + context.arc(canvas_x, canvas_y, canvas_r, 0., TWO_PI).unwrap(); } context.stroke(); }