Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The "Graphing" class.
- import java.awt.*;
- import hsa.Console;
- import java.text.DecimalFormat;
- public class Graphing
- {
- static Console c; // The output console
- public static void main (String[] args)
- {
- c = new Console ();
- greygraph ();
- double leftX = -25;
- double rightX = 25;
- double upY = 25;
- double downY = -25;
- double[] xcor = createXarray (leftX, rightX);
- double[] ycor = createYarray (downY, upY);
- drawYaxis (xcor, ycor);
- drawXaxis (ycor, xcor);
- double[] functionYvalues = new double [501];
- for (int i = 0 ; i < functionYvalues.length ; i++)
- {
- double x = xcor [i];
- functionYvalues [i] = (5*x*x)/(x-1);
- }
- drawFunction (functionYvalues, ycor);
- } // main method
- public static void drawFunction (double[] func, double[] y)
- {
- c.setColor (Color.blue);
- for (int i = 0 ; i < func.length ; i++)
- {
- for (int j = 0 ; j < y.length ; j++)
- {
- for (int j2 = 0 ; j2 < y.length ; j2++)
- {
- if (i < 500 && func [i] > y [0] && func [i + 1] <= y [0])
- {
- if (j2 > 0 && i < 500 && func [i + 1] >= y [j2] && func [i + 1] <= y [j2 - 1])
- {
- c.drawLine (i + 10, 0 + 10, i + 10, j2 + 10);
- }
- }
- else if (i < 500 && func [i + 1] > y [0] && func [i] <= y [0])
- {
- if (j > 0 && func [i] >= y [j] && func [i] <= y [j - 1] && func [i - 1] > func [i])
- {
- c.drawLine (i + 10, j + 10, i + 10, 400 + 10);
- }
- else if (j > 0 && func [i] >= y [j] && func [i] <= y [j - 1] && func [i - 1] < func [i])
- {
- c.drawLine (i + 1 + 10, j + 10, i + 1 + 10, 0 + 10);
- }
- }
- else if (i < 500 && func [i] < y [400] && func [i + 1] >= y [400])
- {
- if (j2 > 0 && i < 500 && func [i + 1] >= y [j2] && func [i + 1] <= y [j2 - 1])
- {
- c.drawLine (i + 1 + 10, 400 + 10, i + 1 + 10, j2 + 10);
- }
- }
- else if (i < 500 && func [i + 1] < y [400] && func [i] >= y [400])
- {
- if (j > 0 && func [i] >= y [j] && func [i] <= y [j - 1] && func [i - 1] < func [i])
- {
- c.drawLine (i + 10, j + 10, i + 10, 0 + 10);
- }
- else if (j > 0 && func [i] >= y [j] && func [i] <= y [j - 1] && func [i - 1] > func [i])
- {
- c.drawLine (i + 10, j + 10, i + 10, 400 + 10);
- }
- }
- else if (j > 0 && func [i] >= y [j] && func [i] <= y [j - 1])
- {
- if (j2 > 0 && i < 500 && func [i + 1] >= y [j2] && func [i + 1] <= y [j2 - 1])
- {
- c.drawLine (i + 10, j + 10, i + 1 + 10, j2 + 10);
- }
- }
- }
- }
- }
- }
- public static void drawXaxis (double[] y, double[] x)
- {
- DecimalFormat df = new DecimalFormat ("#0.##");
- c.setColor (Color.black);
- for (int i = 0 ; i < y.length ; i++)
- {
- if (y [i] == 0)
- {
- c.drawLine (0 + 10, i + 10, 500 + 10, i + 10);
- for (int j = 0 ; j < 501 ; j += 50)
- {
- if (j != 0)
- {
- c.drawString ((df.format (x [j])), j + 5, i + 25);
- }
- else
- {
- c.drawString ((df.format (x [j])), j + 2, i + 25);
- }
- }
- }
- else if (y [i] > 0 && y [i + 1] < 0)
- {
- c.drawLine (0 + 10, i + 10, 500 + 10, i + 10);
- for (int j = 0 ; j < 501 ; j += 50)
- {
- if (j != 0)
- {
- c.drawString ((df.format (x [j])), j + 5, i + 25);
- }
- else
- {
- c.drawString ((df.format (x [j])), j + 2, i + 25);
- }
- }
- }
- }
- }
- public static void drawYaxis (double[] x, double[] y)
- {
- DecimalFormat df = new DecimalFormat ("#0.##");
- c.setColor (Color.black);
- for (int i = 0 ; i < x.length ; i++)
- {
- if (x [i] == 0)
- {
- c.drawLine (i + 10, 0 + 10, i + 10, 400 + 10);
- for (int j = 0 ; j < y.length ; j += 50)
- {
- if (y[j] == 0)
- {
- }
- else if (j != 0)
- {
- if (y [j] >= 0)
- {
- c.drawString ((df.format (y [j])), i - 5, j + 15);
- }
- else
- {
- c.drawString ((df.format (y [j])), i - 9, j + 15);
- }
- }
- else
- {
- c.drawString ((df.format (y [j])), i - 5, j + 15);
- }
- }
- }
- else if (x [i] > 0 && x [i - 1] < 0)
- {
- c.drawLine (i + 10, 0 + 10, i + 10, 400 + 10);
- for (int j = 0 ; j < y.length ; j += 50)
- {
- if (y[j] == 0)
- {
- }
- else if (j != 0)
- {
- if (y [j] >= 0)
- {
- c.drawString ((df.format (y [j])), i - 5, j + 15);
- }
- else
- {
- c.drawString ((df.format (y [j])), i - 9, j + 15);
- }
- }
- else
- {
- c.drawString ((df.format (y [j])), i - 5, j + 15);
- }
- }
- }
- }
- }
- public static double[] createXarray (double x1, double x2)
- {
- double[] array = new double [501];
- double counter = Math.abs (x2 - x1) / (500.00);
- for (int i = 0 ; i <= 500 ; i++)
- {
- array [i] = x1 + (i * counter);
- }
- return array;
- }
- public static double[] createYarray (double y1, double y2)
- {
- double[] array = new double [401];
- double counter = Math.abs (y2 - y1) / (400.00);
- for (int i = 0 ; i <= 400 ; i++)
- {
- array [i] = y2 - (i * counter);
- }
- return array;
- }
- public static void greygraph ()
- {
- c.setColor (Color.lightGray);
- for (int y = 0 ; y <= 350 ; y += 50)
- {
- for (int x = 0 ; x <= 450 ; x += 50)
- {
- c.drawRect (x + 10, y + 10, 50, 50);
- }
- }
- }
- } // Graphing class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement