adjust examples to y-axis-flip
This commit is contained in:
parent
64bd8ee530
commit
346a4fd720
114
turtle-lib/examples/cartesian_axes.rs
Normal file
114
turtle-lib/examples/cartesian_axes.rs
Normal file
@ -0,0 +1,114 @@
|
||||
//! Cartesian coordinate system example
|
||||
//!
|
||||
//! This example draws a cartesian coordinate system with X and Y axes,
|
||||
//! labeled axis endpoints, and 4 labeled points (one in each quadrant).
|
||||
|
||||
use macroquad::prelude::*;
|
||||
use turtle_lib::*;
|
||||
|
||||
#[turtle_main("Cartesian Axes")]
|
||||
fn draw(turtle: &mut TurtlePlan) {
|
||||
const AXIS_LENGTH: f32 = 250.0;
|
||||
const GRID_STEP: f32 = 50.0;
|
||||
const FONT_SIZE: i32 = 16;
|
||||
const SMALL_FONT: i32 = 24;
|
||||
|
||||
// Draw coordinate system center and axes
|
||||
turtle
|
||||
.reset()
|
||||
.set_speed(1100)
|
||||
.pen_up()
|
||||
.go_to(vec2(0.0, 0.0))
|
||||
.pen_down();
|
||||
|
||||
// Draw X axis (horizontal)
|
||||
turtle.set_pen_color(macroquad::prelude::BLACK);
|
||||
turtle.set_pen_width(2.0);
|
||||
turtle.pen_up().go_to(vec2(-AXIS_LENGTH, 0.0)).pen_down();
|
||||
turtle.forward(2.0 * AXIS_LENGTH);
|
||||
|
||||
// Draw Y axis (vertical)
|
||||
turtle.pen_up().go_to(vec2(0.0, -AXIS_LENGTH)).pen_down();
|
||||
turtle.set_heading(90.0);
|
||||
turtle.forward(2.0 * AXIS_LENGTH);
|
||||
|
||||
// Draw axis tick marks (major and minor)
|
||||
turtle.set_pen_width(1.0);
|
||||
const MINOR_STEP: f32 = GRID_STEP / 10.0; // 1/10th markers
|
||||
|
||||
let mut pos = -AXIS_LENGTH;
|
||||
while pos <= AXIS_LENGTH {
|
||||
if (pos - 0.0).abs() > 1.0 {
|
||||
// Skip origin
|
||||
// Determine if this is a major or minor tick
|
||||
let is_major = (pos / GRID_STEP).abs().fract() < 0.01;
|
||||
let tick_length = if is_major { 10.0 } else { 5.0 };
|
||||
|
||||
// X axis tick
|
||||
turtle
|
||||
.pen_up()
|
||||
.set_pen_width(tick_length / 5.0)
|
||||
.go_to(vec2(pos, -tick_length / 2.0))
|
||||
.pen_down()
|
||||
.set_heading(90.0)
|
||||
.forward(tick_length);
|
||||
|
||||
// Y axis tick
|
||||
turtle
|
||||
.pen_up()
|
||||
.go_to(vec2(-tick_length / 2.0, pos))
|
||||
.set_pen_width(tick_length / 5.0)
|
||||
.pen_down()
|
||||
.set_heading(0.0)
|
||||
.forward(tick_length);
|
||||
}
|
||||
pos += MINOR_STEP;
|
||||
}
|
||||
|
||||
// Label axes
|
||||
turtle
|
||||
.pen_up()
|
||||
.go_to(vec2(AXIS_LENGTH + 20.0, 0.0))
|
||||
.set_heading(0.0)
|
||||
.write_text("X", FONT_SIZE);
|
||||
|
||||
turtle
|
||||
.pen_up()
|
||||
.go_to(vec2(0.0, AXIS_LENGTH + 20.0))
|
||||
.set_heading(0.0)
|
||||
.write_text("Y", FONT_SIZE);
|
||||
|
||||
// Draw and label 4 points (one per quadrant)
|
||||
let points = vec![
|
||||
(vec2(120.0, 100.0), "A(2|1)"),
|
||||
(vec2(-120.0, 100.0), "B(-2|1)"),
|
||||
(vec2(-120.0, -100.0), "C(-2|-1)"),
|
||||
(vec2(120.0, -100.0), "D(2|-1)"),
|
||||
];
|
||||
|
||||
for (position, label) in points {
|
||||
// Draw point as small circle
|
||||
turtle
|
||||
.pen_up()
|
||||
.go_to(position)
|
||||
.set_pen_color(macroquad::prelude::RED)
|
||||
.set_pen_width(7.0)
|
||||
.pen_down()
|
||||
.forward(1.0); // Just a small mark
|
||||
|
||||
// Add label
|
||||
let label_offset = vec2(label.len() as f32 * 2.5, 0.0);
|
||||
turtle
|
||||
.pen_up()
|
||||
.go_to(position - label_offset)
|
||||
.set_heading(0.0)
|
||||
.write_text(label, SMALL_FONT);
|
||||
}
|
||||
|
||||
// Label origin
|
||||
turtle
|
||||
.pen_up()
|
||||
.go_to(vec2(-30.0, -20.0))
|
||||
.set_heading(0.0)
|
||||
.write_text("O", FONT_SIZE);
|
||||
}
|
||||
@ -35,7 +35,7 @@ async fn main() {
|
||||
|
||||
// Draw triangular hole in the middle
|
||||
println!("Drawing triangular hole...");
|
||||
turtle.go_to(vec2(200.0, 120.0));
|
||||
turtle.go_to(vec2(200.0, -120.0));
|
||||
turtle.pen_down(); // Start new contour for hole
|
||||
|
||||
for _ in 0..3 {
|
||||
@ -48,7 +48,7 @@ async fn main() {
|
||||
|
||||
// Draw circular hole (top-left) using circle_left
|
||||
println!("Drawing circular hole (top-left) with circle_left...");
|
||||
turtle.go_to(vec2(100.0, 100.0));
|
||||
turtle.go_to(vec2(100.0, -100.0));
|
||||
turtle.pen_down(); // Start new contour for hole
|
||||
turtle.circle_left(30.0, 360.0, 36); // radius=30, full circle, 36 steps
|
||||
println!("Closing circle contour with pen_up");
|
||||
@ -56,7 +56,7 @@ async fn main() {
|
||||
|
||||
// Draw circular hole (bottom-right) using circle_right
|
||||
println!("Drawing circular hole (bottom-right) with circle_right...");
|
||||
turtle.go_to(vec2(280.0, 280.0));
|
||||
turtle.go_to(vec2(280.0, -280.0));
|
||||
turtle.pen_down(); // Start new contour for hole
|
||||
turtle.circle_right(40.0, 360.0, 36); // radius=40, full circle, 36 steps
|
||||
println!("Closing circle contour with pen_up");
|
||||
|
||||
@ -28,7 +28,7 @@ fn draw_cheese(turtle: &mut TurtlePlan) {
|
||||
|
||||
// Draw triangular hole in the middle
|
||||
println!("Drawing triangular hole...");
|
||||
turtle.go_to(vec2(200.0, 120.0));
|
||||
turtle.go_to(vec2(200.0, -120.0));
|
||||
turtle.pen_down(); // Start new contour for hole
|
||||
|
||||
for _ in 0..3 {
|
||||
@ -41,7 +41,7 @@ fn draw_cheese(turtle: &mut TurtlePlan) {
|
||||
|
||||
// Draw circular hole (top-left) using circle_left
|
||||
println!("Drawing circular hole (top-left) with circle_left...");
|
||||
turtle.go_to(vec2(100.0, 100.0));
|
||||
turtle.go_to(vec2(100.0, -100.0));
|
||||
turtle.pen_down(); // Start new contour for hole
|
||||
turtle.circle_left(30.0, 360.0, 36); // radius=30, full circle, 36 steps
|
||||
println!("Closing circle contour with pen_up");
|
||||
@ -49,7 +49,7 @@ fn draw_cheese(turtle: &mut TurtlePlan) {
|
||||
|
||||
// Draw circular hole (bottom-right) using circle_right
|
||||
println!("Drawing circular hole (bottom-right) with circle_right...");
|
||||
turtle.go_to(vec2(280.0, 280.0));
|
||||
turtle.go_to(vec2(280.0, -280.0));
|
||||
turtle.pen_down(); // Start new contour for hole
|
||||
turtle.circle_right(40.0, 360.0, 36); // radius=40, full circle, 36 steps
|
||||
println!("Closing circle contour with pen_up");
|
||||
|
||||
@ -83,8 +83,6 @@ fn run_game_logic(
|
||||
smiley_tx.send(plan.build()).ok();
|
||||
}
|
||||
|
||||
draw_smiley(&smiley_tx, true);
|
||||
|
||||
// Setup: Position hangman turtle and draw base (hill)
|
||||
{
|
||||
let mut plan = create_turtle_plan();
|
||||
|
||||
@ -90,7 +90,7 @@ fn sierpinski_triangle_auto(turtle: &mut TurtlePlan, level: u8) {
|
||||
let size = TRIANGLE_SIZE;
|
||||
|
||||
turtle.pen_up();
|
||||
turtle.go_to((-screen_width() / 2.0 + 20.0, screen_height() / 2.0 - 20.0));
|
||||
turtle.go_to((-screen_width() / 2.0 + 20.0, -screen_height() / 2.0 + 20.0));
|
||||
turtle.set_heading(0.0); // 0 = East (pointing right)
|
||||
|
||||
// The drawing itself.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user