Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #ifndef TURTLE_HH__
  2. #define TURTLE_HH__
  3. #include <iostream>
  4.  
  5.  
  6. // http://paulbourke.net/dataformats/postscript/
  7.  
  8. class Point {
  9. double x_, y_;
  10. public:
  11. Point(double x=0.0, double y=0.0)
  12. : x_(x), y_(y)
  13. {}
  14.  
  15. static Point polar(double angle, double distance);
  16. static Point cartesian(double x, double y);
  17.  
  18. double get_x() const {
  19. return x_;
  20. }
  21. double get_y() const {
  22. return y_;
  23. }
  24.  
  25. Point& operator+=(const Point& other) {
  26. x_ += other.x_;
  27. y_ += other.y_;
  28. return *this;
  29. }
  30.  
  31. Point& operator-=(const Point& other) {
  32. x_ -= other.x_;
  33. y_ -= other.y_;
  34. return *this;
  35. }
  36. };
  37.  
  38. Point operator+(const Point& p1, const Point& p2);
  39. Point operator-(const Point& p1, const Point& p2);
  40.  
  41. std::ostream& operator<<(std::ostream& out, const Point& p);
  42.  
  43. class Color {
  44. double r_, g_, b_;
  45. public:
  46. Color(double r = 0.0, double g = 0.0, double b = 0.0)
  47. : r_(r), g_(g), b_(b)
  48. {}
  49.  
  50. double red() const {
  51. return r_;
  52. }
  53.  
  54. double green() const {
  55. return g_;
  56. }
  57. double blue() const {
  58. return b_;
  59. }
  60.  
  61. static Color gray(double gray){
  62. return Color(gray, gray, gray);
  63. }
  64. };
  65.  
  66. class Turtle {
  67. double width_, height_;
  68. Point pos_;
  69. double heading_;
  70. bool is_pendown_;
  71. Color color_;
  72.  
  73. public:
  74.  
  75. Turtle(double width, double height)
  76. : width_(width), height_(height),
  77. pos_(width_/2.0, height_/2.0),
  78. heading_(0),
  79. is_pendown_(false),
  80. color_(Color(0, 0, 0))
  81. {}
  82.  
  83. virtual ~Turtle() {
  84.  
  85. }
  86.  
  87. virtual void setup() {
  88.  
  89. }
  90.  
  91. double get_width() const {
  92. return width_;
  93. }
  94. double get_height() const {
  95. return height_;
  96. }
  97.  
  98. const Point& get_pos() const {
  99. return pos_;
  100. }
  101.  
  102. double get_heading() const {
  103. return heading_;
  104. }
  105.  
  106. bool is_pendown() const {
  107. return is_pendown_;
  108. }
  109.  
  110. Turtle& set_pos(const Point& p) {
  111. pos_ = p;
  112. return *this;
  113. }
  114.  
  115. Turtle& set_heading(double angle) {
  116. heading_ = angle;
  117. return *this;
  118. }
  119.  
  120. Turtle& penup() {
  121. is_pendown_ = false;
  122. return *this;
  123. }
  124.  
  125. Turtle& pendown() {
  126. is_pendown_ = true;
  127. return *this;
  128. }
  129.  
  130. Turtle& right(double angle) {
  131. heading_ -= angle;
  132. return *this;
  133. }
  134.  
  135. Turtle& left(double angle) {
  136. heading_ += angle;
  137. return *this;
  138. }
  139.  
  140. virtual Turtle& moveto(const Point& pos) {
  141. pos_ = pos;
  142. return *this;
  143. }
  144.  
  145. virtual Turtle& pencolor(const Color& c){
  146. color_ = c;
  147. return *this;
  148. }
  149.  
  150. Turtle& forward(double distance) {
  151. Point movement = Point::polar(heading_, distance);
  152. Point next_pos = get_pos() + movement;
  153. moveto(next_pos);
  154. }
  155.  
  156. Turtle& backward(double distance) {
  157. forward(-distance);
  158. }
  159.  
  160. };
  161.  
  162. class PSTurtle: public Turtle {
  163. std::ostream& out_;
  164.  
  165. public:
  166. PSTurtle(double w, double h, std::ostream& out=std::cout)
  167. : Turtle(w, h), out_(out)
  168. {}
  169.  
  170. virtual void setup();
  171. virtual ~PSTurtle();
  172.  
  173. Turtle& pensize(double width);
  174. Turtle& pencolor(const Color& c);
  175. Turtle& moveto(const Point& p);
  176.  
  177. };
  178.  
  179.  
  180.  
  181. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement