< prev index next >
modules/javafx.graphics/src/main/java/com/sun/marlin/DCollinearSimplifier.java
Print this page
*** 23,52 ****
* questions.
*/
package com.sun.marlin;
- import com.sun.javafx.geom.PathConsumer2D;
! public final class CollinearSimplifier implements PathConsumer2D {
enum SimplifierState {
Empty, PreviousPoint, PreviousLine
};
// slope precision threshold
! static final float EPS = 1e-4f; // aaime proposed 1e-3f
! PathConsumer2D delegate;
SimplifierState state;
! float px1, py1, px2, py2;
! float pslope;
! CollinearSimplifier() {
}
! public CollinearSimplifier init(PathConsumer2D delegate) {
this.delegate = delegate;
this.state = SimplifierState.Empty;
return this; // fluent API
}
--- 23,52 ----
* questions.
*/
package com.sun.marlin;
!
! public final class DCollinearSimplifier implements DPathConsumer2D {
enum SimplifierState {
Empty, PreviousPoint, PreviousLine
};
// slope precision threshold
! static final double EPS = 1e-4D; // aaime proposed 1e-3D
! DPathConsumer2D delegate;
SimplifierState state;
! double px1, py1, px2, py2;
! double pslope;
! DCollinearSimplifier() {
}
! public DCollinearSimplifier init(DPathConsumer2D delegate) {
this.delegate = delegate;
this.state = SimplifierState.Empty;
return this; // fluent API
}
*** 64,104 ****
state = SimplifierState.Empty;
delegate.closePath();
}
@Override
! public void quadTo(float x1, float y1, float x2, float y2) {
emitStashedLine();
delegate.quadTo(x1, y1, x2, y2);
// final end point:
state = SimplifierState.PreviousPoint;
px1 = x2;
py1 = y2;
}
@Override
! public void curveTo(float x1, float y1, float x2, float y2,
! float x3, float y3) {
emitStashedLine();
delegate.curveTo(x1, y1, x2, y2, x3, y3);
// final end point:
state = SimplifierState.PreviousPoint;
px1 = x3;
py1 = y3;
}
@Override
! public void moveTo(float x, float y) {
emitStashedLine();
delegate.moveTo(x, y);
state = SimplifierState.PreviousPoint;
px1 = x;
py1 = y;
}
@Override
! public void lineTo(final float x, final float y) {
switch (state) {
case Empty:
delegate.lineTo(x, y);
state = SimplifierState.PreviousPoint;
px1 = x;
--- 64,104 ----
state = SimplifierState.Empty;
delegate.closePath();
}
@Override
! public void quadTo(double x1, double y1, double x2, double y2) {
emitStashedLine();
delegate.quadTo(x1, y1, x2, y2);
// final end point:
state = SimplifierState.PreviousPoint;
px1 = x2;
py1 = y2;
}
@Override
! public void curveTo(double x1, double y1, double x2, double y2,
! double x3, double y3) {
emitStashedLine();
delegate.curveTo(x1, y1, x2, y2, x3, y3);
// final end point:
state = SimplifierState.PreviousPoint;
px1 = x3;
py1 = y3;
}
@Override
! public void moveTo(double x, double y) {
emitStashedLine();
delegate.moveTo(x, y);
state = SimplifierState.PreviousPoint;
px1 = x;
py1 = y;
}
@Override
! public void lineTo(final double x, final double y) {
switch (state) {
case Empty:
delegate.lineTo(x, y);
state = SimplifierState.PreviousPoint;
px1 = x;
*** 111,121 ****
py2 = y;
pslope = getSlope(px1, py1, x, y);
return;
case PreviousLine:
! final float slope = getSlope(px2, py2, x, y);
// test for collinearity
if ((slope == pslope) || (Math.abs(pslope - slope) < EPS)) {
// merge segments
px2 = x;
py2 = y;
--- 111,121 ----
py2 = y;
pslope = getSlope(px1, py1, x, y);
return;
case PreviousLine:
! final double slope = getSlope(px2, py2, x, y);
// test for collinearity
if ((slope == pslope) || (Math.abs(pslope - slope) < EPS)) {
// merge segments
px2 = x;
py2 = y;
*** 137,150 ****
if (state == SimplifierState.PreviousLine) {
delegate.lineTo(px2, py2);
}
}
! private static float getSlope(float x1, float y1, float x2, float y2) {
! float dy = y2 - y1;
! if (dy == 0f) {
! return (x2 > x1) ? Float.POSITIVE_INFINITY
! : Float.NEGATIVE_INFINITY;
}
return (x2 - x1) / dy;
}
}
--- 137,150 ----
if (state == SimplifierState.PreviousLine) {
delegate.lineTo(px2, py2);
}
}
! private static double getSlope(double x1, double y1, double x2, double y2) {
! double dy = y2 - y1;
! if (dy == 0D) {
! return (x2 > x1) ? Double.POSITIVE_INFINITY
! : Double.NEGATIVE_INFINITY;
}
return (x2 - x1) / dy;
}
}
< prev index next >