Fit y=ax^2+bx through (0,0), (x1,y1), (x2,y2). - Java java.lang

Java examples for java.lang:Math Geometry

Description

Fit y=ax^2+bx through (0,0), (x1,y1), (x2,y2).

Demo Code

/***/*from w  w w  .j a va2s . c o  m*/
 * Copyright (C) 2010 Johan Henriksson
 * This code is under the Endrov / BSD license. See www.endrov.net
 * for the full text and how to cite.
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.vecmath.Vector3d;

public class Main{
    /**
     * Fit y=ax^2+bx through (0,0), (x1,y1), (x2,y2). 
     * @return a,b
     */
    public static Tuple<Double, Double> fitQuadratic(double x1, double y1,
            double x2, double y2) {
        double xSQ = x1 * x1;
        double dSQ = x2 * x2;
        double det = xSQ * x2 - dSQ * x1;

        double a = (y1 * x2 - y2 * x1) / det;
        double b = (y2 * xSQ - y1 * dSQ) / det;
        return Tuple.make(a, b);
    }
}

Related Tutorials