Guest User

Untitled

a guest
Feb 20th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. /* Homework 3
  2. * CS 506 Fall 2006
  3. * Hans Fugal
  4. */
  5.  
  6. import java.awt.*;
  7. import javax.swing.*;
  8.  
  9. /* Problem: You are to write a triangle filling algorithm.
  10. * The only graphics call(other than setColor) you are allowed to
  11. * use at this time is page.drawLine(x1,y1,x2,y2). You may use
  12. * fillOval and drawoval to make an interesting image, but not as a
  13. * part of your fill triangle routine.
  14. */
  15. public class hw3 extends JApplet
  16. {
  17. private Graphics page;
  18.  
  19. public void init()
  20. {
  21. setBackground(Color.gray);
  22. setForeground(Color.black);
  23. setSize(600,600);
  24. }
  25.  
  26. public void paint(Graphics g)
  27. {
  28. page = g;
  29. fill_triangle(300,250,295,50,305,20);
  30. fill_triangle(305,250,315,50,325,50);
  31. fill_triangle(350,300,450,295,495,280);
  32. fill_triangle(350,320,450,315,450,330);
  33. }
  34.  
  35. public static void main(String s[])
  36. {
  37. JFrame f = new JFrame("Homework 3 - Hans Fugal");
  38. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39. JApplet applet = new hw3();
  40. f.getContentPane().add("Center", applet);
  41. applet.init();
  42. f.pack();
  43. f.setSize(new Dimension(610,630));
  44. f.setVisible(true);
  45. }
  46.  
  47. void fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3)
  48. {
  49. int t;
  50.  
  51. // find maxes and mins
  52. int xmin, xmax, ymin, ymax;
  53. xmin = xmax = x1;
  54. if (x2 < xmin)
  55. xmin = x2;
  56. if (x3 < xmin)
  57. xmin = x3;
  58. if (x2 > xmax)
  59. xmax = x2;
  60. if (x3 > xmax)
  61. xmax = x3;
  62. ymin = ymax = y1;
  63. if (y2 < ymin)
  64. ymin = y2;
  65. if (y3 < ymin)
  66. ymin = y3;
  67. if (y2 > ymax)
  68. ymax = y2;
  69. if (y3 > ymax)
  70. ymax = y3;
  71.  
  72. // determine whether steep, and if not swap xs and ys
  73. boolean unsteep = (ymax-ymin) < (xmax-xmin);
  74. if (unsteep)
  75. {
  76. t = x1;
  77. x1 = y1;
  78. y1 = t;
  79.  
  80. t = x2;
  81. x2 = y2;
  82. y2 = t;
  83.  
  84. t = x3;
  85. x3 = y3;
  86. y3 = t;
  87. }
  88.  
  89. // sort points by y
  90. if (y1 > y3)
  91. {
  92. t = x1;
  93. x1 = x3;
  94. x3 = t;
  95.  
  96. t = y1;
  97. y1 = y3;
  98. y3 = t;
  99. }
  100. if (y1 > y2)
  101. {
  102. t = x1;
  103. x1 = x2;
  104. x2 = t;
  105.  
  106. t = y1;
  107. y1 = y2;
  108. y2 = t;
  109. }
  110. if (y2 > y3)
  111. {
  112. t = x2;
  113. x2 = x3;
  114. x3 = t;
  115.  
  116. t = y2;
  117. y2 = y3;
  118. y3 = t;
  119. }
  120.  
  121. // precalculations
  122. int l,r; // left and right x extents
  123. int fl,fr; // fraction
  124. int dxl,dyl,dxr,dyr; // deltas
  125. int sl,sr; // sign of slope (-1 or +1)
  126. l = r = x1;
  127. boolean x2_left = ((x2-x1) < (y2-y1)*(x3-x1)/(y3-y1));
  128. if (x2_left)
  129. {
  130. dxl = x2-x1;
  131. dyl = y2-y1;
  132. dxr = x3-x1;
  133. dyr = y3-y1;
  134. }
  135. else
  136. {
  137. dxl = x3-x1;
  138. dyl = y3-y1;
  139. dxr = x2-x1;
  140. dyr = y2-y1;
  141. }
  142. sl = (dxl < 0 ? -1 : 1);
  143. sr = (dxr < 0 ? -1 : 1);
  144. fl = sl*dyl;
  145. fr = 0;
  146.  
  147. // fill from y1 to y2
  148. if (y1 == y2)
  149. if (unsteep)
  150. page.drawLine(y1,x1,y2,x2);
  151. else
  152. page.drawLine(x1,y1,x2,y2);
  153. else
  154. {
  155. for (int y=y1; y<=y2; y++)
  156. {
  157. if (r<l)r=l;
  158. if (unsteep)
  159. page.drawLine(y,l,y,r);
  160. else
  161. page.drawLine(l,y,r,y);
  162.  
  163. if (y == y2) // don't calculate the next extents if we're done
  164. break;
  165.  
  166. // left edge scan
  167. fl += dxl;
  168. while (Math.abs(fl) > dyl)
  169. {
  170. fl -= sl*dyl;
  171. l += sl;
  172. }
  173.  
  174. // right edge scan
  175. fr += dxr;
  176. while (Math.abs(fr) >= dyr)
  177. {
  178. fr -= sr*dyr;
  179. r += sr;
  180. }
  181. }
  182. }
  183.  
  184. // fill from y2 to y3
  185. if (y2 == y3)
  186. if (unsteep)
  187. page.drawLine(y2,x2, y3,x3);
  188. else
  189. page.drawLine(x2,y2, x3,y3);
  190. else
  191. {
  192. if (x2_left)
  193. {
  194. dxl = x3-x2;
  195. dyl = y3-y2;
  196. sl = (dxl < 0 ? -1 : 1);
  197. fl = 0;
  198. l = x2;
  199. } else {
  200. dxr = x3-x2;
  201. dyr = y3-y2;
  202. sr = (dxr < 0 ? -1 : 1);
  203. fr = 0;
  204. r = x2;
  205. }
  206.  
  207. for (int y=y2; y<=y3; y++)
  208. {
  209. if (r<l)r=l;
  210. if (unsteep)
  211. page.drawLine(y,l,y,r);
  212. else
  213. page.drawLine(l,y,r,y);
  214.  
  215. // left edge scan
  216. fl += dxl;
  217. while (Math.abs(fl) > dyl)
  218. {
  219. fl -= sl*dyl;
  220. l += sl;
  221. }
  222.  
  223. // right edge scan
  224. fr += dxr;
  225. while (Math.abs(fr) >= dyr)
  226. {
  227. fr -= sr*dyr;
  228. r += sr;
  229. }
  230. }
  231. }
  232. }
  233. }
Add Comment
Please, Sign In to add comment