Giter VIP home page Giter VIP logo

c-turtle's Introduction


C-Turtle is a port of Python's Turtle facility for C++11, with the intent of being a close analog to the Python implementation. This package was developed with the intent of student usage under an academic setting, and was designed to be "as easy to use as possible". This package has been released under the MIT license accordingly. Given that this is intended to be an education-oriented package, to ease the setup process it has been created to be Header-Only. Simply copy CTurtle.hpp (alongside CImg.hpp) into your include path or project and you're ready to go.

This package heavily uses CImg for its display and drawing functions. As such, it must be available in the include path alongside CTurtle itself.

Is it "C-Turtle" or "CTurtle"?

Either one works. The "C" prefix is a nod to the single dependency of this project, CImg. CTurtle quickly became the preference in reference to the name of the header file, "CTurtle.hpp", whereas C-Turtle was originally though to be its proper name. As time has progressed, the two spellings have become synonymous in meaning.

Direct Comparison between C++ and Python

The following table contains examples, which do the exact same thing, between C-Turtle and Python's Turtle.

C++ Python
  #include "CTurtle.hpp"

  namespace ct = cturtle;

  int main(int argc, char** argv) {
      ct::TurtleScreen scr;
      ct::Turtle turtle(scr);
      turtle.speed(ct::TS_SLOWEST);
      turtle.fillcolor({"purple"});
      turtle.begin_fill();
      for (int i = 0; i < 4; i++) {
          turtle.forward(50);
          turtle.right(90);
      }
      turtle.end_fill();
      scr.bye();
      return 0;
  }

  
  import turtle

  turt = turtle.Turtle()
  turt.fillcolor("purple")
  turt.speed("slowest")

  turt.begin_fill()
  for i in range(4):
      turt.forward(50)
      turt.right(90)
  turt.end_fill()

  turt.bye()

   

Headless Mode

C-Turtle also supports drawing to an animated GIF instead of a display (e.g, "headless"). This is configurable through the "headless" preprocessor definitions, seen in following example. To write GIFs, C-Turtle uses jo_gif, a wonderful public domain GIF library created by Jon Olick. This avoids having ImageMagick as a dependency, which is what CImg uses by default to save animated GIFs.

In "headless" mode, TurtleScreen has all functionality relating to input and background images removed. This is due to 1) the lack of a display to receive event notifications, and 2) the lack of a guarantee of a safely-usable filesystem to load images from. It does work under the assumption that the filesystem is safe to save to, however.

//Make special note of these defines prior to usage.
#define CTURTLE_HEADLESS //Define to configure CTurtle for Headless mode.

#define CTURTLE_HEADLESS_SAVEDIR "./test.gif" //Optional define, default is "./cturtle.gif".
#define CTURTLE_HEADLESS_WIDTH 800 //Optional define, default is 400
#define CTURTLE_HEADLESS_HEIGHT 600 //Optional define, default is 300

#include "CTurtle.hpp"

namespace ct = cturtle;

int main(int argc, char** argv) {
    ct::TurtleScreen scr;
    ct::Turtle rt(scr);

    for(int i = 0; i < 4; i++){
        rt.forward(50);
        rt.right(90);
    }

    scr.bye();
    return 0;
}

Why does headless mode take so long to save a GIF file?

A frame is added to the resulting GIF for every change in state for a Turtle. This includes rotation, pen changes, size changes, etcetera. You can choose to display only every N frames, and thus save only every N frames, by taking advantage of tracer settings (see tracer(int countmax, unsigned int delayMS) function in TurtleScreen documentation). This dramatically reduces file size and write time in exchange for less frames in the image.

Why does headless mode print HTML + Base64 by default?

Headless mode was developed with the intention of being embedded in web applications, namely Runestone Interactive textbooks. As such, it prints HTML to display the results of the executed code by printing a Base64-encoded version of the resulting GIF file. This lets CTurtle be very easily embedded without needing any extra tricks or external File IO with any kind of backend. This can be disabled by having #define CTURTLE_HEADLESS_NO_HTML before the inclusion of CTurtle.

Examples and Derivative Works

Packaged alongside CTurtle

These examples can be found in the examples directory at the root of this repository. Many are derived from Runestone Interactive textbooks, such as the Sierpinski Triangle, Knight's Tour, Multiple Turtles, and Recursion Tree examples. Others, such as the Koch Fractal examples, are derived from Berea College coursework and were manually converted from Python.

Derivative Works

The following four works are shared with permission of their creator, Dr. Mark Liu, from the University of Kentucky. These are fantastic examples of games, and voice-controlled play provides a particularly interesting change from command-line or button-based interfaces.

Student Work Showcase

As time progresses, and as I am afforded the opportunity, I will provide visual examples of work students have done using this library. None of these are produced by my own work (but did use C-Turtle in their generation), however permission was given to post the works here!

Three examples of artwork generated in the style of Piet Mondrian as part of an assignment on Recursion.  

Azis Toktobaev - Berea College

Mondrian Art Example 1  

Bryar Frank - Berea College

Mondrian Art Example 2  

Karina Agliullova - Berea College

Mondrian Art Example 3

c-turtle's People

Contributors

walkerje avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

c-turtle's Issues

The "circle" function of Turtle is inconsistent with Python.

In Python, the circle function could do two kinds of jobs.

  1. draw a full circle with given radius. e.g. turtle.circle(100)
  2. draw an arc with given radius and extent. e.g. turtle.circle(100, 135)

It seems like C-Turtle's circle function can only do the first one. Is there any way to achieve the second intention?

"Undo" redraw performance is slow.

"Undo" operations force a canvas invalidation due to top-level object removal.

This means that the entire screen-- everything every turtle on the screen has drawn up to that point-- must be completely re-drawn to ensure consistent results after the top-level object's removal.

This often leads to "Undo" operations occurring MUCH slower than other operations, especially with more complicated scenes (thick lines are the dominant culprit of this).

Some code that exemplifies this issue is below.

#include "CTurtle.hpp"
namespace ct = cturtle;

int main(int argc, char** argv) {
    ct::TurtleScreen scr;
    ct::Turtle turtle(scr);
    turtle.speed(ct::TS_FASTEST);
    turtle.setundobuffer(2000);//make undo buffer larger than we need

    for(int i = 0; i < 100; i++){
        turtle.width(i);
        //Comment-out the above line to see difference substantial difference in speed for the undo operations.

        turtle.forward(((i*5)-(40-i)));
        turtle.right(144);
    }

    //Loops until finished undoing.
    while(turtle.undo());

    scr.exitonclick();
    return 0;
}

Off-By-One Rounding issues in Transform object.

Straight, single-pixel-width lines are often off-by-one on their target coordinates.

This is likely due to some rounding error in point transformation (e.g, the operator() overload in Transform, or the transform function of the same object), however, I have been unable to find the ultimate cause of this issue.

Below is code to reproduce this issue, alongside a screenshot exhibiting this issue, created by the example code.
Example Image

#include "CTurtle.hpp"
namespace ct = cturtle;

int main(int argc, char** argv) {
    ct::TurtleScreen scr;
    ct::Turtle turtle(scr);
    turtle.speed(ct::TS_FASTEST);

    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            turtle.penup();
            turtle.goTo(j*50,i*50);
            turtle.pendown();

            turtle.fillcolor({"random"});
            turtle.pencolor({"random"});
            turtle.begin_fill();
            for(int k = 0; k < 4; k++){
                turtle.forward(50);
                turtle.right(90);
            }
            turtle.end_fill();
        }
    }

    scr.exitonclick();
    return 0;
}

Arch Linux Support

I really like the idea of this repo, but I'd like a version that does not use the libx11-dev package (not available in arch). Can you please help?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.