examples.geometry.demos.CubicCurveDeCasteljauExample.java Source code

Java tutorial

Introduction

Here is the source code for examples.geometry.demos.CubicCurveDeCasteljauExample.java

Source

/*******************************************************************************
 * Copyright (c) 2011, 2012 itemis AG and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Matthias Wienand (itemis AG) - initial API and implementation
 *     
 *******************************************************************************/
package examples.geometry.demos;

import org.eclipse.gef4.geometry.planar.CubicCurve;
import org.eclipse.gef4.geometry.planar.Line;
import org.eclipse.gef4.geometry.planar.Point;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.event.shared.EventBus;

import examples.geometry.AbstractExample;
import examples.geometry.CanvasDrawer;
import examples.geometry.ControllableShape;

public class CubicCurveDeCasteljauExample extends AbstractExample {

    private double parameterValue = 0.5;

    public CubicCurveDeCasteljauExample(EventBus eventBus, Canvas canvas) {
        super(eventBus, canvas);
    }

    @Override
    protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
        return new ControllableShape[] { new ControllableShape(canvas, eventBus) {
            {
                /*
                 * These are the control points used to construct the CubicCurve
                 * later.
                 */
                addControlPoints(new Point(100, 200), new Point(200, 100), new Point(300, 300),
                        new Point(400, 200));
            }

            @Override
            public CubicCurve getShape() {
                /*
                 * Constructs the CubicCurve of the defined control points.
                 */
                return new CubicCurve(getPoints());
            }

            @Override
            public void onDraw(Canvas canvas) {
                Context2d context = canvas.getContext2d();
                /*
                 * Draws the CubicCurve and the de Casteljau construction for
                 * the current parameter value.
                 */

                // Construct the CubicCurve from the defined control points.
                CubicCurve curve = getShape();
                CanvasDrawer.strokePath(curve.toPath(), context);

                /*
                 * Retrieve control points to compute the linear interpolations
                 * of the de Casteljau algorithm.
                 */
                Point[] points = getPoints();

                /*
                 * Define the colors for the intermediate lines. We have three
                 * stages and therefore three different colors for a cubic
                 * Bezier curve. This is the case, because the de Casteljau
                 * algorithm reduces the number of control points in each
                 * iteration until it reaches the actual point on the curve.
                 */
                String[] colors = new String[] { "green", "blue", "red" };

                for (int ci = 0; ci < colors.length; ci++) {
                    for (int i = 0; i < 3 - ci; i++) {
                        context.beginPath();
                        context.moveTo(points[i].x, points[i].y);
                        context.lineTo(points[i + 1].x, points[i + 1].y);
                        context.setStrokeStyle(colors[ci]);
                        context.stroke();
                        context.closePath();

                        // interpolate point for the next iteration
                        points[i] = new Line(points[i], points[i + 1]).get(parameterValue);

                        // draw point                  
                        context.beginPath();
                        context.arc(points[i].x, points[i].y, 2, 0, 180);
                        context.setStrokeStyle("black");
                        context.stroke();
                        context.closePath();
                    }
                }
            }
        } };
    }

}