Forums - Open Redstone Engineers

Full Version: Formula finder
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.

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
That beautiful code :O
Now find the piecewise function of a quadratic spline interpolation with the inputs being multiple points >:3
(02-06-2014, 11:51 PM)Iceglade Wrote: [ -> ]That beautiful code :O

Was that sarcastic?
No, I can't write java for crap xD
(02-08-2014, 12:33 PM)Iceglade Wrote: [ -> ]No, I can't write java for crap xD

It's pretty easy really, but xeo would kill me if I said I like java, so even though I like it I'm not going to tell anybody.
>post on ORE forums

>not going to tell anybody

>...? XD
(02-08-2014, 01:44 PM)Iceglade Wrote: [ -> ]>post on ORE forums

>not going to tell anybody

>...? XD

Because that's how I roll!
sosneaky
(02-09-2014, 04:09 AM)VirtualPineapple Wrote: [ -> ]sosneaky

ikr