Advertisement
Guest User

Iamgayasfuck

a guest
Aug 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. // The "Graphing" class.
  2. import java.awt.*;
  3. import hsa.Console;
  4. import java.text.DecimalFormat;
  5.  
  6. public class Graphing
  7. {
  8. static Console c; // The output console
  9.  
  10. public static void main (String[] args)
  11. {
  12. c = new Console ();
  13.  
  14. greygraph ();
  15.  
  16. double leftX = -25;
  17. double rightX = 25;
  18. double upY = 25;
  19. double downY = -25;
  20.  
  21. double[] xcor = createXarray (leftX, rightX);
  22. double[] ycor = createYarray (downY, upY);
  23.  
  24. drawYaxis (xcor, ycor);
  25.  
  26.  
  27.  
  28. drawXaxis (ycor, xcor);
  29.  
  30. double[] functionYvalues = new double [501];
  31.  
  32. for (int i = 0 ; i < functionYvalues.length ; i++)
  33. {
  34. double x = xcor [i];
  35. functionYvalues [i] = (5*x*x)/(x-1);
  36. }
  37.  
  38. drawFunction (functionYvalues, ycor);
  39.  
  40. } // main method
  41.  
  42.  
  43. public static void drawFunction (double[] func, double[] y)
  44. {
  45. c.setColor (Color.blue);
  46. for (int i = 0 ; i < func.length ; i++)
  47. {
  48. for (int j = 0 ; j < y.length ; j++)
  49. {
  50. for (int j2 = 0 ; j2 < y.length ; j2++)
  51. {
  52. if (i < 500 && func [i] > y [0] && func [i + 1] <= y [0])
  53. {
  54. if (j2 > 0 && i < 500 && func [i + 1] >= y [j2] && func [i + 1] <= y [j2 - 1])
  55. {
  56. c.drawLine (i + 10, 0 + 10, i + 10, j2 + 10);
  57. }
  58. }
  59. else if (i < 500 && func [i + 1] > y [0] && func [i] <= y [0])
  60. {
  61. if (j > 0 && func [i] >= y [j] && func [i] <= y [j - 1] && func [i - 1] > func [i])
  62. {
  63. c.drawLine (i + 10, j + 10, i + 10, 400 + 10);
  64. }
  65.  
  66. else if (j > 0 && func [i] >= y [j] && func [i] <= y [j - 1] && func [i - 1] < func [i])
  67. {
  68. c.drawLine (i + 1 + 10, j + 10, i + 1 + 10, 0 + 10);
  69. }
  70. }
  71.  
  72. else if (i < 500 && func [i] < y [400] && func [i + 1] >= y [400])
  73. {
  74. if (j2 > 0 && i < 500 && func [i + 1] >= y [j2] && func [i + 1] <= y [j2 - 1])
  75. {
  76. c.drawLine (i + 1 + 10, 400 + 10, i + 1 + 10, j2 + 10);
  77. }
  78. }
  79. else if (i < 500 && func [i + 1] < y [400] && func [i] >= y [400])
  80. {
  81. if (j > 0 && func [i] >= y [j] && func [i] <= y [j - 1] && func [i - 1] < func [i])
  82. {
  83. c.drawLine (i + 10, j + 10, i + 10, 0 + 10);
  84. }
  85.  
  86. else if (j > 0 && func [i] >= y [j] && func [i] <= y [j - 1] && func [i - 1] > func [i])
  87. {
  88. c.drawLine (i + 10, j + 10, i + 10, 400 + 10);
  89. }
  90. }
  91. else if (j > 0 && func [i] >= y [j] && func [i] <= y [j - 1])
  92. {
  93. if (j2 > 0 && i < 500 && func [i + 1] >= y [j2] && func [i + 1] <= y [j2 - 1])
  94. {
  95. c.drawLine (i + 10, j + 10, i + 1 + 10, j2 + 10);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102.  
  103.  
  104. public static void drawXaxis (double[] y, double[] x)
  105. {
  106. DecimalFormat df = new DecimalFormat ("#0.##");
  107. c.setColor (Color.black);
  108. for (int i = 0 ; i < y.length ; i++)
  109. {
  110. if (y [i] == 0)
  111. {
  112. c.drawLine (0 + 10, i + 10, 500 + 10, i + 10);
  113. for (int j = 0 ; j < 501 ; j += 50)
  114. {
  115. if (j != 0)
  116. {
  117. c.drawString ((df.format (x [j])), j + 5, i + 25);
  118. }
  119. else
  120. {
  121. c.drawString ((df.format (x [j])), j + 2, i + 25);
  122. }
  123.  
  124. }
  125. }
  126. else if (y [i] > 0 && y [i + 1] < 0)
  127. {
  128. c.drawLine (0 + 10, i + 10, 500 + 10, i + 10);
  129. for (int j = 0 ; j < 501 ; j += 50)
  130. {
  131. if (j != 0)
  132. {
  133. c.drawString ((df.format (x [j])), j + 5, i + 25);
  134. }
  135. else
  136. {
  137. c.drawString ((df.format (x [j])), j + 2, i + 25);
  138. }
  139.  
  140. }
  141. }
  142. }
  143. }
  144.  
  145.  
  146. public static void drawYaxis (double[] x, double[] y)
  147. {
  148. DecimalFormat df = new DecimalFormat ("#0.##");
  149. c.setColor (Color.black);
  150. for (int i = 0 ; i < x.length ; i++)
  151. {
  152. if (x [i] == 0)
  153. {
  154. c.drawLine (i + 10, 0 + 10, i + 10, 400 + 10);
  155. for (int j = 0 ; j < y.length ; j += 50)
  156. {
  157. if (y[j] == 0)
  158. {
  159.  
  160. }
  161. else if (j != 0)
  162. {
  163. if (y [j] >= 0)
  164. {
  165. c.drawString ((df.format (y [j])), i - 5, j + 15);
  166. }
  167. else
  168. {
  169. c.drawString ((df.format (y [j])), i - 9, j + 15);
  170. }
  171. }
  172. else
  173. {
  174. c.drawString ((df.format (y [j])), i - 5, j + 15);
  175. }
  176.  
  177. }
  178. }
  179. else if (x [i] > 0 && x [i - 1] < 0)
  180. {
  181. c.drawLine (i + 10, 0 + 10, i + 10, 400 + 10);
  182. for (int j = 0 ; j < y.length ; j += 50)
  183. {
  184. if (y[j] == 0)
  185. {
  186.  
  187. }
  188. else if (j != 0)
  189. {
  190. if (y [j] >= 0)
  191. {
  192. c.drawString ((df.format (y [j])), i - 5, j + 15);
  193. }
  194. else
  195. {
  196. c.drawString ((df.format (y [j])), i - 9, j + 15);
  197. }
  198. }
  199. else
  200. {
  201. c.drawString ((df.format (y [j])), i - 5, j + 15);
  202. }
  203.  
  204. }
  205.  
  206. }
  207. }
  208. }
  209.  
  210.  
  211. public static double[] createXarray (double x1, double x2)
  212. {
  213. double[] array = new double [501];
  214.  
  215. double counter = Math.abs (x2 - x1) / (500.00);
  216.  
  217. for (int i = 0 ; i <= 500 ; i++)
  218. {
  219. array [i] = x1 + (i * counter);
  220. }
  221. return array;
  222. }
  223.  
  224.  
  225. public static double[] createYarray (double y1, double y2)
  226. {
  227. double[] array = new double [401];
  228.  
  229. double counter = Math.abs (y2 - y1) / (400.00);
  230.  
  231. for (int i = 0 ; i <= 400 ; i++)
  232. {
  233. array [i] = y2 - (i * counter);
  234. }
  235. return array;
  236. }
  237.  
  238.  
  239. public static void greygraph ()
  240. {
  241. c.setColor (Color.lightGray);
  242.  
  243. for (int y = 0 ; y <= 350 ; y += 50)
  244. {
  245. for (int x = 0 ; x <= 450 ; x += 50)
  246. {
  247. c.drawRect (x + 10, y + 10, 50, 50);
  248. }
  249. }
  250. }
  251. } // Graphing class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement