Skip to content
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
2 changes: 1 addition & 1 deletion demo/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
67 changes: 52 additions & 15 deletions src/turtle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -79,13 +80,40 @@ 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.
*/
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`.
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion src/turtle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down