Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * @(#)SplineInterpolator.java  1.0  September 9, 2007
   *
   * Copyright (c) 2011 Werner Randelshofer
   * Hausmatt 10, CH-6405 Goldau, Switzerland
   * All rights reserved.
   *
   * The copyright of this software is owned by Werner Randelshofer. 
   * You may not use, copy or modify this software, except in  
  * accordance with the license agreement you entered into with  
  * Werner Randelshofer. For details see accompanying license terms. 
  */
 package org.monte.media;
 
A bezier interpolator for use in conjunction with an Animator object.

This class interpolates fractional values using a Bezier spline. The anchor points for the spline are assumed to be (0, 0) and (1, 1). Control points should all be in the range [0, 1].

FIXME - This interpolator does not work!

Author(s):
Werner Randelshofer
Version:
$Id: BezierInterpolator.java 299 2013-01-03 07:40:18Z werner $
 
 public class BezierInterpolator extends AbstractSplineInterpolator {

    
Note: (x0,y0) and (x1,y1) are implicitly (0, 0) and (1,1) respectively.
 
     private double[] controlPoints;

    
Creates a new instance of SplineInterpolator with the control points defined by (x1, y1) and (x2, y2). The anchor points are implicitly defined as (0, 0) and (1, 1).

The interpolator runs for one second.

Parameters:
x1 The x coordinate for the first bezier control point.
y1 The y coordinate for the first bezier control point.
x2 The x coordinate for the second bezier control point.
y2 The x coordinate for the second bezier control point.
Throws:
java.lang.IllegalArgumentException This exception is thrown when values beyond the allowed [0,1] range are passed in
 
     public BezierInterpolator(float x1float y1float x2float y2) {
         this(x1y1x2y2, 1000);
 
     }

    
Creates a new instance of SplineInterpolator with the control points defined by (x1, y1) and (x2, y2). The anchor points are implicitly defined as (0, 0) and (1, 1).

The interpolator runs for the specified time span.

Parameters:
x1 The x coordinate for the first bezier control point.
y1 The y coordinate for the first bezier control point.
x2 The x coordinate for the second bezier control point.
y2 The x coordinate for the second bezier control point.
timespan The time span in milliseconds.
Throws:
java.lang.IllegalArgumentException This exception is thrown when values beyond the allowed [0,1] range are passed in
 
     public BezierInterpolator(float x1float y1float x2float y2long timespan) {
         this(x1y1x2y2falsetimespan);
     }

    
Creates a new instance of SplineInterpolator with the control points defined by (x1, y1) and (x2, y2). The anchor points are implicitly defined as (0, 0) and (1, 1).

The interpolator runs for the specified time span.

Parameters:
x1 The x coordinate for the first bezier control point.
y1 The y coordinate for the first bezier control point.
x2 The x coordinate for the second bezier control point.
y2 The x coordinate for the second bezier control point.
reverse Run interpolator in the reverse direction.
timespan The time span in milliseconds.
Throws:
java.lang.IllegalArgumentException This exception is thrown when values beyond the allowed [0,1] range are passed in
 
     public BezierInterpolator(float x1float y1float x2float y2boolean reverselong timespan) {
         super((reverse) ? 1f : 0f, (reverse) ? 0f : 1f, timespan);
 
         if (x1 < 0 || x1 > 1.0f
                 || y1 < 0 || y1 > 1.0f
                || x2 < 0 || x2 > 1.0f
                || y2 < 0 || y2 > 1.0f) {
            throw new IllegalArgumentException("Control points must be in "
                    + "the range [0, 1]:");
        }
        =new double[4*2];
        [0] = 0;
        [1] = 0;
        [2] = x1;
        [3] = y1;
        [4] = x2;
        [5] = y2;
        [6] = 1;
        [7] = 1;
        updateFractions(100);
    }
    
    
Interpolates between the specified control points.

Parameters:
controlPoints The control points of the bezier path. Must include the first and the last control point. The curve must be in the range [0,1].
    public BezierInterpolator(double[][] controlPoints) {
        this(controlPoints,false,1000);
           
        
    }
    
Interpolates between the specified control points.

Parameters:
controlPoints The control points of the bezier path. Must include the first and the last control point. The curve must be in the range [0,1].
    public BezierInterpolator(double[][] controlPointsboolean reverselong timespan) {
        super((reverse) ? 1f : 0f, (reverse) ? 0f : 1f, timespan);
        this.=new double[controlPoints.length*2];
        for (int i=0;i<controlPoints.length;i++){
            this.[i*2]=controlPoints[i][0];
            this.[i*2+1]=controlPoints[i][1];
        }
        updateFractions(100);
    }

    
Evaluates the bezier function and returns a 2D point.

Returns:
A point, with x- and y-coordinates for time t.
    @Override
    public Point2D.Float getXY(float tPoint2D.Float xy) {
        if (xy==null)xy=new Point2D.Float(0,0);
        
        double[] p = .clone();
        for (int i = p.length/2-1; i > 0; i--) {
            for (int j = 0; j < ij++) {
                p[j*2+0] = (1 - t) * p[j*2+0] + t * p[(j+1)*2+0];
                p[j*2+1] = (1 - t) * p[j*2+1] + t * p[(j + 1)*2+1];
            }
        }
        
        xy.setLocation(p[0],p[1]);
        return xy;
    }
    
Evaluates the bezier function and returns a 2D point.

Returns:
A point, with x- and y-coordinates for time t.
    @Override
    public float getY(float t) {
        double[] p = .clone();
        for (int i = p.length/2-1; i > 0; i--) {
            for (int j = 0; j < ij++) {
                p[j*2+0] = (1 - t) * p[j*2+0] + t * p[(j + 1)*2+0];
                p[j*2+1] = (1 - t) * p[j*2+1] + t * p[(j + 1)*2+1];
            }
        }
        return (float)p[1];
    }
New to GrepCode? Check out our FAQ X