From 40f4b39ce9210e0052b2be157503dc7269d68376 Mon Sep 17 00:00:00 2001 From: Limnanthes Serafini Date: Wed, 1 Nov 2023 01:46:50 -0700 Subject: [PATCH] Added arc function and improved make dependencies --- demo/Makefile | 2 +- src/turtle.cpp | 67 +++++++++++++++++++++++++++++++++++++++----------- src/turtle.hpp | 7 +++++- 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/demo/Makefile b/demo/Makefile index 4b03478..2086fd8 100644 --- a/demo/Makefile +++ b/demo/Makefile @@ -1,6 +1,6 @@ all: demo -demo: demo.o +demo: demo.o ../src/libturtle.a ../src/libpoint.a ../libembroidery/libembroidery.a clang++ demo.o ../src/libturtle.a ../src/libpoint.a ../libembroidery/libembroidery.a -o demo demo.o: demo.cpp diff --git a/src/turtle.cpp b/src/turtle.cpp index 19f87d2..4ea1790 100644 --- a/src/turtle.cpp +++ b/src/turtle.cpp @@ -69,6 +69,7 @@ void Turtle::satinoff() { */ void Turtle::pendown() { pen_is_down_ = true; + embPattern_addStitchAbs(emb_, position_.x_, position_.y_, 0, color_); } /** Puts the turtle's pen up. @@ -79,6 +80,18 @@ void Turtle::penup() { pen_is_down_ = false; } +/** Sets the state of the turtle's pen. + * If given a true input, puts the pen down. + * If given false, puts the pen up. +*/ +void Turtle::setpen(bool down) { + if (down) { + pendown(); + return; + } + penup(); +} + /** Returns the Turtle's current position. * The position is returned as a Point object. */ @@ -86,6 +99,21 @@ Point Turtle::position() { return position_; } +/** Returns the Turtle's current direction. + * The direction is returned as a Point object. +*/ +Point Turtle::dir() { + return dir_; +} + +/** Returns the state of the Turtle's pen. + * If the pen is down, returns True. + * Else, returns False. +*/ +bool Turtle::ispendown() { + return pen_is_down_; +} + // Functions for turning the Turtle /** Turn to face `pos`. @@ -207,6 +235,30 @@ void Turtle::backward(const float dist) { move(dir_ * dist * -1); } +/** Draws an arc from the Turtle's current position and direction. + * If degrees is positive, the arc is drawn clockwise - + * if degrees is negative, then the arc is counterclockwise. +*/ +void Turtle::arc(const float radius, const float degrees) { + float radians = M_PI * degrees / 180; + float arc_len = abs(radians * radius); + + int arc_sides = 2 + int(fmin(11+abs(radius)/6.0, 59.0)) * abs(degrees / 360); + float w = degrees / arc_sides; + float l = arc_len / arc_sides; + for (int i = 0; i < arc_sides; i++) { + forward(l); + turn(w); + } +} +/** Alias for a clockwise arc of 360 degrees. +*/ +void Turtle::circle(const float radius) { + Point start = position_; + arc(radius, 360); + gotopoint(start); +} + // Functions for drawing text /** Draws the text `message`. @@ -374,21 +426,6 @@ void Turtle::rectangle(float w, float h) { right(90); } - -void Turtle::circle(float radius) { - Point start = position_; - int steps = 2+int(fmin(11+abs(radius)/6.0, 59.0)); - float w = 360.0 / steps; - float l = 2.0 * abs(radius) / steps; - - for (int i=0; i < steps; ++i) { - forward(l); - turn(w); - } - - gotopoint(start); -} - void Turtle::snowflake(float sidelength, int levels) { flakeside(sidelength, levels); left(120); diff --git a/src/turtle.hpp b/src/turtle.hpp index 5276152..3793c55 100644 --- a/src/turtle.hpp +++ b/src/turtle.hpp @@ -16,7 +16,10 @@ class Turtle { void satinoff(); void pendown(); void penup(); + void setpen(bool isdown); Point position(); + Point dir(); + bool ispendown(); void turntoward(const Point& pos); void turntoward(const float x, const float y); @@ -33,6 +36,9 @@ class Turtle { void forward(const float dist); void backward(const float dist); + void arc(const float radius, const float degrees); + void circle(const float radius); + void displayMessage(std::string message, float scale); void save(std::string fname); @@ -52,7 +58,6 @@ class Turtle { void increment_x(const float x); void increment_y(const float y); // void rectangle(float w, float h); - // void circle(float radius); // void snowflake(float sidelength, int levels); // void flakeside(float sidelength, int levels); // void squareSpiral(int line);