Java examples for 2D Graphics:Curve
Gets a Point2D on the given CubicCurve2D at the parameterized position t.
/*//from ww w.jav a2 s. c o m * Copyright (C) 2005 Jordan Kiang * jordan-at-kiang.org * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ //package com.java2s; import java.awt.geom.CubicCurve2D; import java.awt.geom.Point2D; public class Main { /** * Gets a Point2D on the given CubicCurve2D at the parameterized position t. * * @param curve the curve * @param t a parameterized t value along the length of the curve, 0-1 inclusive * @return the point */ static public Point2D getPointOnCubicCurve(CubicCurve2D curve, double t) { if (null == curve) { throw new NullPointerException("curve must be non-null!"); } else if (t < 0.0 || t > 1.0) { throw new IllegalArgumentException("t must be between 0 and 1!"); } double ax = getCubicAx(curve); double bx = getCubicBx(curve); double cx = getCubicCx(curve); double ay = getCubicAy(curve); double by = getCubicBy(curve); double cy = getCubicCy(curve); double tSquared = t * t; double tCubed = t * tSquared; double x = (ax * tCubed) + (bx * tSquared) + (cx * t) + curve.getX1(); double y = (ay * tCubed) + (by * tSquared) + (cy * t) + curve.getY1(); return new Point2D.Double(x, y); } static private double getCubicAx(CubicCurve2D curve) { return curve.getX2() - curve.getX1() - getCubicBx(curve) - getCubicCx(curve); } static private double getCubicBx(CubicCurve2D curve) { return 3.0 * (curve.getCtrlX2() - curve.getCtrlX1()) - getCubicCx(curve); } static private double getCubicCx(CubicCurve2D curve) { return 3.0 * (curve.getCtrlX1() - curve.getX1()); } static private double getCubicAy(CubicCurve2D curve) { return curve.getY2() - curve.getY1() - getCubicBy(curve) - getCubicCy(curve); } static private double getCubicBy(CubicCurve2D curve) { return 3.0 * (curve.getCtrlY2() - curve.getCtrlY1()) - getCubicCy(curve); } static private double getCubicCy(CubicCurve2D curve) { return 3.0 * (curve.getCtrlY1() - curve.getY1()); } }