hitlerdidnothingwron

Untitled

Nov 11th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. /**
  2. * Circle.cpp
  3. *
  4. * EECS 183, Fall 2016
  5. * Project 4: CoolPics
  6. *
  7. * John Dorsey, Patrick Ahimovic
  8. * jsdorsey, paddya
  9. *
  10. * Functions for the shape subclass of Circle
  11. */
  12.  
  13. #include "Circle.h"
  14. #include "Line.h"
  15. #include "Graphics.h"
  16. #include "utility.h"
  17. #include <algorithm>
  18. using namespace std;
  19.  
  20. Circle::Circle() {
  21. radius = 0;
  22. }
  23. Circle::Circle(Point pt, int r, Color c) {
  24. setCenter(pt);
  25. setRadius(r);
  26. setColor(c);
  27. }
  28.  
  29. void Circle::setCenter(Point new_pt) {
  30. center = new_pt;
  31. }
  32.  
  33. void Circle::setColor(Color new_col) {
  34. color = new_col;
  35. }
  36.  
  37. void Circle::setRadius(int new_r) {
  38. radius = new_r;
  39. }
  40.  
  41. Point Circle::getCenter() {
  42. return center;
  43. }
  44.  
  45. int Circle::getRadius() {
  46. return radius;
  47. }
  48.  
  49. Color Circle::getColor() {
  50. return color;
  51. }
  52.  
  53. void Circle::read(istream& ins) {
  54.  
  55. Point centRead;
  56. int radRead;
  57. Color colorRead;
  58.  
  59. ins >> centRead >> radRead >> colorRead;
  60.  
  61. center = centRead;
  62. radius = radRead;
  63. color = colorRead;
  64. }
  65.  
  66. void Circle::write(ostream& outs) {
  67.  
  68. Point w_cent = getCenter();
  69. int w_rad = getRadius();
  70. Color w_col = getColor();
  71.  
  72. outs << " " << w_cent << " " << w_rad << " " << w_col << endl;
  73.  
  74. }
  75.  
  76. // Your code goes above this line.
  77. // Don't change the implementations below!
  78.  
  79. istream& operator >> (istream& ins, Circle& circle)
  80. {
  81. circle.read(ins);
  82. return ins;
  83. }
  84.  
  85. ostream& operator << (ostream& outs, Circle circle)
  86. {
  87. circle.write(outs);
  88. return outs;
  89. }
  90.  
  91. void Circle::draw(Graphics & drawer)
  92. {
  93. int radius = min(getRadius(), (int)DIMENSION);
  94. int error = -radius;
  95. int x = radius;
  96. int y = 0;
  97. Color c = getColor();
  98.  
  99. while (x >= y)
  100. {
  101. plot8points(x, y, c, drawer);
  102.  
  103. error += y;
  104. ++y;
  105. error += y;
  106.  
  107. if (error >= 0)
  108. {
  109. error -= x;
  110. --x;
  111. error -= x;
  112. }
  113. }
  114. }
  115.  
  116. int Circle::checkRadius(int radius)
  117. {
  118. if (radius < 0)
  119. {
  120. return -1 * radius;
  121. }
  122. return radius;
  123. }
  124.  
  125. void Circle::plot8points(int x, int y, Color c, Graphics& drawer)
  126. {
  127. plot4points(x, y, c, drawer);
  128. if (x != y) plot4points(y, x, c, drawer);
  129. }
  130.  
  131. void Circle::plot4points(int x, int y, Color c, Graphics& drawer)
  132. {
  133. // cx and cy denote the offset of the circle center from the origin.
  134. int cx = getCenter().getX();
  135. int cy = getCenter().getY();
  136.  
  137. Point pt1Start(cx - x, cy + y);
  138. Point pt1End(cx + x, cy + y);
  139. Line line1(pt1Start, pt1End, c);
  140. line1.draw(drawer);
  141.  
  142. Point pt2Start(cx - x, cy - y);
  143. Point pt2End(cx + x, cy - y);
  144. Line line2(pt2Start, pt2End, c);
  145. line2.draw(drawer);
  146. }
Add Comment
Please, Sign In to add comment