Rotates a set of JavaFX PathElement primitives around an implied origin of 0,0 by angle theta in *RADIANS* - changing the primitives IN PLACE. - Java javafx.scene.shape

Java examples for javafx.scene.shape:Path

Description

Rotates a set of JavaFX PathElement primitives around an implied origin of 0,0 by angle theta in *RADIANS* - changing the primitives IN PLACE.

Demo Code


//package com.java2s;

import java.util.List;
import javafx.scene.shape.ArcTo;

import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;

import javafx.scene.shape.PathElement;

public class Main {
    public static void main(String[] argv) throws Exception {
        List pathSegment = java.util.Arrays.asList("asdf", "java2s.com");
        double theta = 2.45678;
        rotatePathElementsAroundPointInPlace(pathSegment, theta);
    }//from w w  w  . ja  v  a  2 s .c  o  m

    public static void rotatePathElementsAroundPointInPlace(
            List<PathElement> pathSegment, double theta) {
        double cTheta = Math.cos(theta);
        double sTheta = Math.sin(theta);
        for (PathElement shp : pathSegment) {
            if (shp instanceof MoveTo) {
                MoveTo mtShp = (MoveTo) shp;
                double vx = mtShp.getX();
                double vy = mtShp.getY();
                double x = roundIfClose((cTheta * vx) - (sTheta * vy));
                double y = roundIfClose((sTheta * vx) + (cTheta * vy));
                System.out.println(mtShp.getClass().getSimpleName() + " "
                        + vx + " " + vy + " " + x + " " + y);
                mtShp.setX(x);
                mtShp.setY(y);
            } else if (shp instanceof LineTo) {
                LineTo lineShp = (LineTo) shp;
                double vx = lineShp.getX();
                double vy = lineShp.getY();
                // Rounding so float errors rounded rather than truncated.
                double x = roundIfClose((cTheta * vx) - (sTheta * vy));
                double y = roundIfClose((sTheta * vx) + (cTheta * vy));
                System.out.println(lineShp.getClass().getSimpleName() + " "
                        + vx + " " + vy + " " + x + " " + y);
                lineShp.setX(x);
                lineShp.setY(y);
            } else if (shp instanceof ArcTo) {
                ArcTo arcShp = (ArcTo) shp;
                double vx = arcShp.getX();
                double vy = arcShp.getY();
                double x = roundIfClose((cTheta * vx) - (sTheta * vy));
                double y = roundIfClose((sTheta * vx) + (cTheta * vy));
                System.out.println(arcShp.getClass().getSimpleName() + " "
                        + vx + " " + vy + " " + x + " " + y);
                arcShp.setX(x);
                arcShp.setY(y);
                // arcShp.getRadiusX(), arcShp.getRadiusY(), arcShp.isSweepFlag();
            } // ???
        }
        System.out.println();
    }

    /**
     * Local function to round "sufficiently close" Number values to whole
     * numbers, to prevent unnecessary aliasing in drawing shapes.
     *
     * @param val
     * @return
     */
    protected static double roundIfClose(double val) {
        double nVal = Math.round(val);
        if (Math.abs(val - nVal) < 0.001) {
            return nVal;
        } else {
            return val;
        }
    }
}

Related Tutorials