Interpolates given points by a bezier curve : Curve « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » CurveScreenshots 
Interpolates given points by a bezier curve
   
/*
 * Copyright (c) 2005 David Benson
 *  
 * See LICENSE file in distribution for licensing details of this source file
 */

import java.awt.Point;
import java.awt.geom.Point2D;

/**
 * Interpolates given points by a bezier curve. The first
 * and the last two points are interpolated by a quadratic
 * bezier curve; the other points by a cubic bezier curve.
 
 * Let p a list of given points and b the calculated bezier points,
 * then one get the whole curve by:
 
 * sharedPath.moveTo(p[0])
 * sharedPath.quadTo(b[0].x, b[0].getY(), p[1].x, p[1].getY());
 
 * for(int i = 2; i < p.length - 1; i++ ) {
 *    Point b0 = b[2*i-3];
 *    Point b1 = b[2*i-2];
 *    sharedPath.curveTo(b0.x, b0.getY(), b1.x, b1.getY(), p[i].x, p[i].getY());
 * }
 
 * sharedPath.quadTo(b[b.length-1].x, b[b.length-1].getY(), p[n - 1].x, p[n - 1].getY());
 
 @author krueger
 */
public class Bezier {

  private static final float AP = 0.5f;
  private Point2D[] bPoints;

  /**
   * Creates a new Bezier curve.
   @param points
   */
  public Bezier(Point2D[] points) {
    int n = points.length;
    if (n < 3) {
      // Cannot create bezier with less than 3 points
      return;
    }
    bPoints = new Point[(n - 2)];
    double paX, paY;
    double pbX = points[0].getX();
    double pbY = points[0].getY();
    double pcX = points[1].getX();
    double pcY = points[1].getY();
    for (int i = 0; i < n - 2; i++) {
      paX = pbX;
      paY = pbY;
      pbX = pcX;
      pbY = pcY;
      pcX = points[i + 2].getX();
      pcY = points[i + 2].getY();
      double abX = pbX - paX;
      double abY = pbY - paY;
      double acX = pcX - paX;
      double acY = pcY - paY;
      double lac = Math.sqrt(acX * acX + acY * acY);
      acX = acX /lac;
      acY = acY /lac;

      double proj = abX * acX + abY * acY;
      proj = proj < ? -proj : proj;
      double apX = proj * acX;
      double apY = proj * acY;

      double p1X = pbX - AP * apX;
      double p1Y = pbY - AP * apY;
      bPoints[* inew Point((intp1X, (intp1Y);

      acX = -acX;
      acY = -acY;
      double cbX = pbX - pcX;
      double cbY = pbY - pcY;
      proj = cbX * acX + cbY * acY;
      proj = proj < ? -proj : proj;
      apX = proj * acX;
      apY = proj * acY;

      double p2X = pbX - AP * apX;
      double p2Y = pbY - AP * apY;
      bPoints[* i + 1new Point((intp2X, (intp2Y);
    }
  }

  /**
   * Returns the calculated bezier points.
   @return the calculated bezier points
   */
  public Point2D[] getPoints() {
    return bPoints;
  }

  /**
   * Returns the number of bezier points.
   @return number of bezier points
   */
  public int getPointCount() {
    return bPoints.length;
  }

  /**
   * Returns the bezier points at position i.
   @param i
   @return the bezier point at position i
   */
  public Point2D getPoint(int i) {
    return bPoints[i];
  }

}

   
    
    
  
Related examples in the same category
1.Move the curve control point and redraw the curve
2.Draw curve with mouseDraw curve with mouse
3.Curve with QuadCurve2DCurve with QuadCurve2D
4.A spline factory instance
5.Spline 2D
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.