Skip to content

Add some spiral examples #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//Faux-bonacci Spiral Example
//Maybe ot quite a true Fibonacci spiral but pretty close
use turtle::{Drawing, ExportError};


fn main() -> Result<(), ExportError> {

let mut fval = 0.0;
let mut drawing = Drawing::new();
let mut turtle = drawing.add_turtle();

for i in 0..=960 {
turtle.forward(fval);
turtle.right(1.0);

//Adjust the tightness of the spiral ex. 0.00001-0.00003
//Change sign to change direction.

fval += 0.0000168 * i as f64;

}

turtle.hide();
drawing.save_svg("fibspiral.svg")?;
Ok(())
}
24 changes: 24 additions & 0 deletions examples/logspiral.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//Logarithmic Spiral Example

use turtle::{Drawing, ExportError};

fn main() -> Result<(), ExportError> {
let mut fval = 0.0;
let mut drawing = Drawing::new();
let mut turtle = drawing.add_turtle();
turtle.set_speed("instant");


for _i in 0..=8192 {
turtle.forward(fval);
turtle.right(1.0);

//Adjust the tightness of the spiral ex. 0.0011 - 0.0009
//Change sign to change direction
fval -= 0.001;
}

turtle.hide();
drawing.save_svg("logspiral.svg")?;
Ok(())
}
40 changes: 40 additions & 0 deletions examples/three_d_logspirals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//3d Logarithmic Spiral Example
//This takes a little while so
// turtle.set_speed("instant");

use turtle::{Drawing, ExportError};

fn main() -> Result<(), ExportError> {

let mut fval = 0.0;
let tval = 1.0;
let mut drawing = Drawing::new();
let mut turtle = drawing.add_turtle();
turtle.set_speed("instant");
for _j in 0..=3 {
for _i in 0..=8192 {
turtle.forward(fval);
turtle.right(tval);

//Adjust the tightness of the spiral
fval -= 0.0009;
}
turtle.right(90.0);
turtle.forward(0.1);
turtle.right(90.0);

for _i in 0..=8192 {
turtle.forward(fval);
turtle.left(tval);

//Adjust the tightness of the spiral
fval += 0.0009;
}
turtle.left(90.0);
turtle.forward(0.1);
turtle.left(90.0);
}
turtle.hide();
drawing.save_svg("three_d_logspiral.svg")?;
Ok(())
}
111 changes: 111 additions & 0 deletions examples/ulamspiral.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//Ulam Spiral
//A plot of primes along a square spiral as such
//where each side length is repeated once then 1
//space is added to grow.
//
//55555555555555555
//5
//5 3.3.3.3.3.4
//5 3 4
//5 3 111 4
//5 3 1 2 4
//5 3 2 4
//5 322222 4
//5 4
//44444444444444
use turtle::{Drawing, ExportError};

fn main() -> Result<(), ExportError> {
//Each ring half grows by 1.0 per 'next' which is
//described in the above image. Each half is 2 sides.

//step is the ring spacing size.

//count is the total steps drawn and what
//is determined to be prime or not.

let mut drawing = Drawing::new();
let mut turtle = drawing.add_turtle();
turtle.set_speed("instant");
let mut count: f64 = 1.0;
let step = 4.0;
let mut next = 1;

//size of spiral in lines x2
for _i in 0..=192 { //Adjust to size
let mut cvr = next.clone(); //a clean copy
while cvr != 0 { //decrementor for count against the side len
let mut result = true; //holds prime check
if count >= 2.0 { //min check value
let sqrt = count.sqrt(); //Find square root of count
let mut trsqrt = sqrt.clone().trunc() + 1.0; //truncate, round up
while trsqrt >= 2.0 {
//Check all the modulus of the real values from the
//rounded up square root down to 2.
//If any == 0.0 then it is composite, else prime.
if count % trsqrt == 0.0 {
result = false;
}
trsqrt -= 1.0; //down to 2
}
if count == 2.0 { // 2 is prime
result = true;
}
}
if count <= 1.0 { // 1 is not prime
result = false;
}

//If prime then draw a larger space then for the other spaces.
//Then restore the size for the next draw.
//If composite then just draw normally.
if result == true {
turtle.set_pen_size(2.0);
turtle.forward(step);
turtle.set_pen_size(1.0);
} else {
turtle.forward(step);
}
cvr -= 1; //decrementor to zer0
count += 1.0; //increment the total count
}

//When done with the side above then turn and repeat.
turtle.right(90.0);
cvr = next.clone();
while cvr != 0 {
let mut result2 = true;
if count >= 2.0 {
let sqrt = count.sqrt();
let mut trsqrt = sqrt.clone().trunc() + 1.0;
while trsqrt >= 2.0 {
if count % trsqrt == 0.0 {
result2 = false;
}
trsqrt -= 1.0;
}
if count == 2.0 {
result2 = true;
}
}
if count <= 1.0 {
result2 = false;
}
if result2 == true {
turtle.set_pen_size(2.0);
turtle.forward(step);
turtle.set_pen_size(1.0);
} else {
turtle.forward(step);
}
cvr -= 1;
count += 1.0;
}
turtle.right(90.0);
next += 1;//Increment the side counter
}
//Save the picture.
turtle.hide();
drawing.save_svg("ulamspiral.svg")?;
Ok(())
}
Loading