Forums - Open Redstone Engineers
Formula finder - Printable Version

+- Forums - Open Redstone Engineers (https://forum.openredstone.org)
+-- Forum: Off-Topic (https://forum.openredstone.org/forum-4.html)
+--- Forum: Programming (https://forum.openredstone.org/forum-8.html)
+--- Thread: Formula finder (/thread-2336.html)



Formula finder - David - 02-06-2014

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


RE: Formula finder - Iceglade - 02-06-2014

That beautiful code :O


RE: Formula finder - VirtualPineapple - 02-07-2014

Now find the piecewise function of a quadratic spline interpolation with the inputs being multiple points >:3


RE: Formula finder - David - 02-07-2014

(02-06-2014, 11:51 PM)Iceglade Wrote: That beautiful code :O

Was that sarcastic?


RE: Formula finder - Iceglade - 02-08-2014

No, I can't write java for crap xD


RE: Formula finder - David - 02-08-2014

(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.


RE: Formula finder - Iceglade - 02-08-2014

>post on ORE forums

>not going to tell anybody

>...? XD


RE: Formula finder - David - 02-08-2014

(02-08-2014, 01:44 PM)Iceglade Wrote: >post on ORE forums

>not going to tell anybody

>...? XD

Because that's how I roll!


RE: Formula finder - VirtualPineapple - 02-09-2014

sosneaky


RE: Formula finder - David - 02-09-2014

(02-09-2014, 04:09 AM)VirtualPineapple Wrote: sosneaky

ikr