02-06-2014, 08:02 PM
Hello! When I was bored today I was like, let's see if I can understand Bresenhams line drawing algorithm! NOPE! Just to up my ego and feel very smart a made something in java. If you give this program two points it'll return a linear formula calculated with the two points it's been given.
If you were to input for example: "3,10" and "5,2", it'll return "y = -4.0x + 22.0". That is the line where both 3,10 and 5,2 are on.
It should be pretty easy to figure out how I did this. If you don't understand this (which I highly doubt) I can create a vid to explain it.
-David
Code:
package david.main;
import java.util.Scanner;
public class Main {
//Create object of the built-in class Scanner which allows me to get the UI.
private static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
//Get the first point.
String point1raw = scan.next();
String[] point1str = point1raw.split(",");
int[] point1 = {Integer.parseInt(point1str[0]), Integer.parseInt(point1str[1])};
//Get the second point
String point2raw = scan.next();
String[] point2str = point2raw.split(",");
int[] point2 = {Integer.parseInt(point2str[0]), Integer.parseInt(point2str[1])};
//y = ax + b
//Calculate a
double a = ((double)point2[1]-(double)point1[1])/((double)point2[0]-(double)point1[0]);
//Calculate b
double b = (double)point1[1]-((double)point1[0]*a);
//Some schmexxyyy displaying stuff.
if(b > 0)
System.out.println("y = " + a + "x + " + b); else if(b < 0) System.out.println("y = " + a + "x - " + -b); else System.out.println("y = " + a + "x");
}
}
If you were to input for example: "3,10" and "5,2", it'll return "y = -4.0x + 22.0". That is the line where both 3,10 and 5,2 are on.
It should be pretty easy to figure out how I did this. If you don't understand this (which I highly doubt) I can create a vid to explain it.
-David