/* The MIT License (MIT) Copyright (c) 2016 Leon Tabak Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package barycentric; /** * This program is practice for the first examination. * * Read more about the mathematics * here. * * @author Leon Tabak * @version 11 February 2016 */ public class Barycentric { public static double alpha(double x, double y, double x0, double y0, double x1, double y1, double x2, double y2) { double numerator = (y1 - y0) * x + (x0 - x1) * y + y0 * x1 - x0 * y1; double denominator = (y1 - y0) * x2 + (x0 - x1) * y2 + y0 * x1 - x0 * y1; return numerator / denominator; } // alpha( double, double, double, double, double, double ) public static double beta(double x, double y, double x0, double y0, double x1, double y1, double x2, double y2) { double numerator = (y2 - y1) * x + (x1 - x2) * y + y1 * x2 - x1 * y2; double denominator = (y2 - y1) * x0 + (x1 - x2) * y0 + y1 * x2 - x1 * y2; return numerator / denominator; } // beta( double, double, double, double, double, double ) public static double gamma(double x, double y, double x0, double y0, double x1, double y1, double x2, double y2) { double numerator = (y0 - y2) * x + (x2 - x0) * y + y2 * x0 - x2 * y0; double denominator = (y0 - y2) * x1 + (x2 - x0) * y1 + y2 * x0 - x2 * y0; return numerator / denominator; } // gamma( double, double, double, double, double, double ) public static void main(String[] args) { double x0 = 0.0; double y0 = 0.0; double x1 = 4.0; double y1 = 0.0; double x2 = 0.0; double y2 = 3.0; double x = x0; double y = y0; System.out.println("alpha(x0,y0) = " + alpha(x, y, x0, y0, x1, y1, x2, y2)); x = x1; y = y1; System.out.println("alpha(x1,y1) = " + alpha(x, y, x0, y0, x1, y1, x2, y2)); x = x2; y = y2; System.out.println("alpha(x2,y2) = " + alpha(x, y, x0, y0, x1, y1, x2, y2)); x = x1; y = y1; System.out.println("beta(x1,y1) = " + beta(x, y, x0, y0, x1, y1, x2, y2)); x = x2; y = y2; System.out.println("beta(x2,y2) = " + beta(x, y, x0, y0, x1, y1, x2, y2)); x = x0; y = y0; System.out.println("beta(x0,y0) = " + beta(x, y, x0, y0, x1, y1, x2, y2)); x = x2; y = y2; System.out.println("gamma(x2,y2) = " + gamma(x, y, x0, y0, x1, y1, x2, y2)); x = x0; y = y0; System.out.println("gamma(x0,y0) = " + gamma(x, y, x0, y0, x1, y1, x2, y2)); x = x1; y = y1; System.out.println("gamma(x1,y1) = " + gamma(x, y, x0, y0, x1, y1, x2, y2)); x = 2.0; y = 1.0; double a = alpha(x, y, x0, y0, x1, y1, x2, y2); double b = beta(x, y, x0, y0, x1, y1, x2, y2); double g = gamma(x, y, x0, y0, x1, y1, x2, y2); System.out.println("alpha(6.0,8.0) = " + a); System.out.println("beta(6.0,8.0) = " + b); System.out.println("gamma(6.0,8.0) = " + g); double difference = 1.0 - a - b; System.out.println("1 - alpha - beta = " + difference); } // main( String [] ) } // Barycentric