Skip to content

Commit

Permalink
PathIter: Added getTransform()
Browse files Browse the repository at this point in the history
Transform: Added transformX(), transformY()
  • Loading branch information
reportmill committed Sep 27, 2021
1 parent 244f482 commit ee449be
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 34 deletions.
81 changes: 47 additions & 34 deletions src/snap/geom/PathIter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public PathIter() { }
*/
public PathIter(Transform aTrans) { _trans = aTrans; }

/**
* Returns the Transform associated with this PathIter.
*/
public Transform getTransform() { return _trans; }

/**
* Returns the winding - how a path determines what to fill when segments intersect.
*/
Expand All @@ -33,16 +38,16 @@ public PathIter() { }
/**
* Returns the next segment.
*/
public abstract Seg getNext(double coords[]);
public abstract Seg getNext(double[] coords);

/**
* Returns the next segment (float coords).
*/
public Seg getNext(float coords[])
public Seg getNext(float[] coords)
{
double dcoords[] = new double[6];
double[] dcoords = new double[6];
Seg seg = getNext(dcoords);
for (int i=0;i<6;i++)
for (int i = 0; i < 6; i++)
coords[i] = (float) dcoords[i];
return seg;
}
Expand All @@ -55,60 +60,67 @@ public Seg getNext(float coords[])
/**
* Returns a MoveTo for given coords.
*/
protected final Seg moveTo(double aX, double aY, double coords[])
protected final Seg moveTo(double aX, double aY, double[] coords)
{
coords[0] = aX; coords[1] = aY;
if (_trans!=null)
coords[0] = aX;
coords[1] = aY;
if (_trans != null)
_trans.transform(coords, 1);
return Seg.MoveTo;
}

/**
* Returns a LineTo for given coords.
*/
protected final Seg lineTo(double aX, double aY, double coords[])
protected final Seg lineTo(double aX, double aY, double[] coords)
{
coords[0] = aX; coords[1] = aY;
if (_trans!=null)
coords[0] = aX;
coords[1] = aY;
if (_trans != null)
_trans.transform(coords, 1);
return Seg.LineTo;
}

/**
* Returns a QuadTo for given coords.
*/
protected final Seg quadTo(double aCPX, double aCPY, double aX, double aY, double coords[])
protected final Seg quadTo(double aCPX, double aCPY, double aX, double aY, double[] coords)
{
coords[0] = aCPX; coords[1] = aCPY;
coords[2] = aX; coords[3] = aY;
if (_trans!=null)
coords[0] = aCPX;
coords[1] = aCPY;
coords[2] = aX;
coords[3] = aY;
if (_trans != null)
_trans.transform(coords, 2);
return Seg.QuadTo;
}

/**
* Returns a CubicTo for given coords.
*/
protected final Seg cubicTo(double aCPX0, double aCPY0, double aCPX1, double aCPY1, double aX, double aY, double coords[])
protected final Seg cubicTo(double aCPX0, double aCPY0, double aCPX1, double aCPY1, double aX, double aY, double[] coords)
{
coords[0] = aCPX0; coords[1] = aCPY0;
coords[2] = aCPX1; coords[3] = aCPY1;
coords[4] = aX; coords[5] = aY;
if (_trans!=null)
coords[0] = aCPX0;
coords[1] = aCPY0;
coords[2] = aCPX1;
coords[3] = aCPY1;
coords[4] = aX;
coords[5] = aY;
if (_trans != null)
_trans.transform(coords, 3);
return Seg.CubicTo;
}

/**
* Returns a CubicTo for start, corner and end points.
*/
protected final Seg arcTo(double lx, double ly, double cx, double cy, double x, double y, double coords[])
protected final Seg arcTo(double lx, double ly, double cx, double cy, double x, double y, double[] coords)
{
double magic = .5523f; // I calculated this in mathematica one time - probably only valid for 90 deg corner.
double cpx1 = lx + (cx-lx)*magic;
double cpy1 = ly + (cy-ly)*magic;
double cpx2 = x + (cx-x)*magic;
double cpy2 = y + (cy-y)*magic;
double cpx1 = lx + (cx - lx) * magic;
double cpy1 = ly + (cy - ly) * magic;
double cpx2 = x + (cx - x) * magic;
double cpy2 = y + (cy - y) * magic;
return cubicTo(cpx1, cpy1, cpx2, cpy2, x, y, coords);
}

Expand All @@ -123,7 +135,7 @@ protected final Seg arcTo(double lx, double ly, double cx, double cy, double x,
public static Rect getBounds(PathIter aPathIter)
{
// Get iter vars
double pts[] = new double[6];
double[] pts = new double[6];
double lx = 0, ly = 0;
Rect bounds = new Rect();
Rect bnds = null;
Expand All @@ -133,17 +145,17 @@ public static Rect getBounds(PathIter aPathIter)
switch (aPathIter.getNext(pts)) {
case MoveTo:
if (bnds==null) {
bounds.setRect(lx=pts[0],ly=pts[1],0,0);
bounds.setRect(lx = pts[0], ly = pts[1],0,0);
continue;
}
case LineTo:
bnds = Line.getBounds(lx, ly, lx=pts[0], ly=pts[1], bnds);
bnds = Line.getBounds(lx, ly, lx = pts[0], ly = pts[1], bnds);
break;
case QuadTo:
bnds = Quad.getBounds(lx, ly, pts[0], pts[1], lx=pts[2], ly=pts[3], bnds);
bnds = Quad.getBounds(lx, ly, pts[0], pts[1], lx = pts[2], ly = pts[3], bnds);
break;
case CubicTo:
bnds = Cubic.getBounds(lx, ly, pts[0], pts[1], pts[2], pts[3], lx=pts[4], ly=pts[5], bnds);
bnds = Cubic.getBounds(lx, ly, pts[0], pts[1], pts[2], pts[3], lx = pts[4], ly = pts[5], bnds);
break;
case Close: break;
}
Expand All @@ -162,7 +174,7 @@ public static Rect getBounds(PathIter aPathIter)
public static double getArcLength(PathIter aPathIter)
{
// Get iter vars
double pts[] = new double[6];
double[] pts = new double[6];
double lx = 0, ly = 0;
double lenAll = 0;
double len = 0;
Expand All @@ -171,17 +183,18 @@ public static double getArcLength(PathIter aPathIter)
while (aPathIter.hasNext()) {
switch (aPathIter.getNext(pts)) {
case MoveTo:
lx = pts[0]; ly = pts[1];
lx = pts[0];
ly = pts[1];
len = 0;
break;
case LineTo:
len = Point.getDistance(lx, ly, lx=pts[0], ly=pts[1]);
len = Point.getDistance(lx, ly, lx = pts[0], ly = pts[1]);
break;
case QuadTo:
len = SegmentLengths.getArcLengthQuad(lx, ly, pts[0], pts[1], lx=pts[2], ly=pts[3]);
len = SegmentLengths.getArcLengthQuad(lx, ly, pts[0], pts[1], lx = pts[2], ly = pts[3]);
break;
case CubicTo:
len = SegmentLengths.getArcLengthCubic(lx, ly, pts[0], pts[1], pts[2], pts[3], lx=pts[4], ly=pts[5]);
len = SegmentLengths.getArcLengthCubic(lx, ly, pts[0], pts[1], pts[2], pts[3], lx = pts[4], ly = pts[5]);
break;
case Close: len = 0; break;
}
Expand Down
16 changes: 16 additions & 0 deletions src/snap/geom/Transform.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,22 @@ public Point transform(Point aPoint, Point aDest)
return aDest;
}

/**
* Transforms the given X value.
*/
public final double transformX(double aX, double aY)
{
return aX * _a + aY * _c + _tx;
}

/**
* Transforms the given Y value.
*/
public final double transformY(double aX, double aY)
{
return aX * _b + aY * _d + _ty;
}

/**
* Transforms the given size.
*/
Expand Down

0 comments on commit ee449be

Please sign in to comment.