From bbbe3dafac03db335c15f1998e16b0ffb8cf95fc Mon Sep 17 00:00:00 2001 From: Cole Lameyer Date: Sun, 12 Nov 2023 15:15:29 -0800 Subject: [PATCH] Added curving --- demo/demo.cpp | 29 +++++++++++++++++++++++------ src/turtle.cpp | 26 +++++++++++++++++++++++++- src/turtle.hpp | 2 ++ 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/demo/demo.cpp b/demo/demo.cpp index eca9fc1..ad1b6fc 100644 --- a/demo/demo.cpp +++ b/demo/demo.cpp @@ -18,14 +18,31 @@ void rect(Turtle& t, float width, float height) { t.right(90); } +void softrect(Turtle& t, float width, float height) { + t.forward(width); + t.rcurve(90, 10); + t.forward(height); + t.rcurve(90, 10); + t.forward(width); + t.rcurve(90, 10); + t.forward(height); + t.rcurve(90, 10); +} void meetTurtle() { - Turtle t; - t.satinon(0.3); - t.pendown(); - rect(t, 20, 30); - t.end(); - t.save("demo.dst"); + Turtle rt; + rt.satinon(0.3); + rt.pendown(); + rect(rt, 20, 30); + rt.end(); + rt.save("demo_rect.dst"); + + Turtle srt; + srt.satinon(0.3); + srt.pendown(); + softrect(srt, 20, 30); + srt.end(); + srt.save("demo_softrect.dst"); } int main() { diff --git a/src/turtle.cpp b/src/turtle.cpp index 19f87d2..e07b3f2 100644 --- a/src/turtle.cpp +++ b/src/turtle.cpp @@ -122,7 +122,7 @@ void Turtle::setdir(const float x, const float y) { setdir(Point(x, y)); } -/** Turns the Turle `degreesccw` degrees. */ +/** Turns the Turtle `degreesccw` degrees. */ void Turtle::turn(const float degreesccw) { float radcw = -degreesccw / 180.0 * 3.141592653589; dir_ = Point(cos(radcw) * dir_.x_ - sin(radcw) * dir_.y_, @@ -207,6 +207,30 @@ void Turtle::backward(const float dist) { move(dir_ * dist * -1); } +/** Rightward curve + * Curves the turtle to the right along a calculated path + */ +void Turtle::rcurve(int degree, int len) { + //len is basically the step count + float turnGradient = degree / len; + for (int i=0;i