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
29 changes: 23 additions & 6 deletions demo/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
26 changes: 25 additions & 1 deletion src/turtle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_,
Expand Down Expand Up @@ -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<len;i++) {
turn(turnGradient);
move(dir_ * 1);
}
}

/** Leftward curve
* Curves the turtle to the right along a calculated path
*/
void Turtle::lcurve(int degree, int len) {
//len is basically the step count
float turnGradient = degree / len;
for (int i=0;i<len;i++) {
turn(-turnGradient);
move(dir_ * 1);
}
}

// Functions for drawing text

/** Draws the text `message`.
Expand Down
2 changes: 2 additions & 0 deletions src/turtle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Turtle {
void turn(const float degreesccw);
void right(float degreesccw);
void left(float degreesccw);
void rcurve(int degree, int len);
void lcurve(int degree, int len);

void move(const Point& delta);
void move(const float x, const float y);
Expand Down