Created a function for the conversion of coordinates from algebra space to screen space. Typo.

This commit is contained in:
2025-08-20 20:48:21 -05:00
parent 87965cafb5
commit 9f5775a99d
2 changed files with 14 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
//Licensed from ganja.js (https://github.com/enkimute/ganja.js) under the MIT License //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 //https://github.com/enkimute/ganja.js/blob/6e97cb45d780cd7c661d9240f1610eb005707417/codegen/rust/cga.rs
// 3D Projective Geometric Algebra // 3D Projective Geometric Algebra

View File

@@ -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 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) { fn draw_point(context: &CanvasRenderingContext2d, point: &CGA) {
//Naively assume we are given a valid point //Naively assume we are given a valid point
let x = point[1]*RADIUS + CENTER_X; let (x,y) = point_to_screen_space(point[E1], point[E2]);
let y = point[2]*RADIUS + CENTER_Y;
context.begin_path(); context.begin_path();
context.set_fill_style_str(RED); context.set_fill_style_str(RED);
@@ -125,8 +130,10 @@ fn draw_line(context: &CanvasRenderingContext2d, line: &CGA) {
let p1y = py + ty * 1000.; let p1y = py + ty * 1000.;
let p2x = px - tx * 1000.; let p2x = px - tx * 1000.;
let p2y = py - ty * 1000.; let p2y = py - ty * 1000.;
context.move_to(p1x*RADIUS + CENTER_X, p1y*RADIUS + CENTER_Y); let (canvas_x1, canvas_y1) = point_to_screen_space(p1x, p1y);
context.line_to(p2x*RADIUS + CENTER_X, p2y*RADIUS + CENTER_Y); 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 { } else {
@@ -139,12 +146,11 @@ fn draw_line(context: &CanvasRenderingContext2d, line: &CGA) {
//draw_point(context, &point_to_cga(c_x, c_y)); //draw_point(context, &point_to_cga(c_x, c_y));
//Convert into Canvas Space //Convert into Canvas Space
let canvas_cx = c_x*RADIUS + CENTER_X; let (canvas_x, canvas_y) = point_to_screen_space(c_x, c_y);
let canvas_cy = c_y*RADIUS + CENTER_Y;
let canvas_r = r*RADIUS; let canvas_r = r*RADIUS;
//Draw //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(); context.stroke();
} }