Skip to content

Commit

Permalink
-Added Geometry.drawArcJoinedLines which will render a Point2DArray i…
Browse files Browse the repository at this point in the history
…nto the given PathPartList using the provided radius
  • Loading branch information
Mark Proctor committed Mar 27, 2015
1 parent 70d9f0c commit 801cdf8
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 82 deletions.
Expand Up @@ -702,11 +702,11 @@ private final boolean parse(final Attributes attr)

if (radius > 0)
{
addLinePoints(linept, p1, radius);
Geometry.drawArcJoinedLines(m_list, Geometry.getPoints(linept, p1), radius);
}
else
{
addLinePoints(linept);
drawLines(linept);
}
return true;
}
Expand All @@ -715,7 +715,8 @@ private final boolean parse(final Attributes attr)
return false;
}

private final void addLinePoints(final NFastDoubleArrayJSO points)

private final void drawLines(final NFastDoubleArrayJSO points)
{
final int size = points.size();

Expand All @@ -725,65 +726,6 @@ private final void addLinePoints(final NFastDoubleArrayJSO points)
}
}

private final void addLinePoints(final NFastDoubleArrayJSO points, Point2D p0, final double radius)
{
final int size = points.size();

int i = 0;

Point2D p1 = new Point2D(points.get(i++), points.get(i++));

Point2D p2 = new Point2D(p1);

Point2D dv0 = p1.sub(p0);

Point2D dx0 = dv0.unit(); // unit vector in the direction of p2

p1 = p1.sub(dx0.mul(radius));

m_list.L(p1.getX(), p1.getY());

for (; i < size; i += 2)
{
Point2D p4 = new Point2D(points.get(i), points.get(i + 1));

drawLines(m_list, p0, p2, p4, radius, size, i + 2);

p0 = p2;

p2 = p4;
}
}

private static void drawLines(PathPartList list, Point2D p0, Point2D p2, Point2D p4, double corner, final int size, int index)
{
Point2D dv1 = p2.sub(p4);

Point2D dx1 = dv1.unit();

Point2D p3 = p2.sub(dx1.mul(corner));

double a1 = Geometry.getAngleBetweenTwoLines(p0, p2, p2, p4);

double a2 = Geometry.RADIANS_90 - (a1 / 2);

double radius = Geometry.getLengthFromASA(Geometry.RADIANS_90, corner, a2);

if (index < size)
{
// p4 always needs to stop short, unless we are at the last point
p4 = p4.add(dx1.mul(corner));
}
drawLine(list, p2, p3, p4, radius);
}

private static void drawLine(PathPartList list, Point2D p2, Point2D p3, Point2D p4, double radius)
{
list.A(p2.getX(), p2.getY(), p3.getX(), p3.getY(), radius);

list.L(p4.getX(), p4.getY());
}

/**
* Returns this OrthogonalPolyLine's points.
*
Expand Down
77 changes: 61 additions & 16 deletions src/main/java/com/ait/lienzo/client/core/shape/PolyLine.java
Expand Up @@ -24,8 +24,11 @@
import com.ait.lienzo.client.core.shape.json.validators.ValidationContext;
import com.ait.lienzo.client.core.shape.json.validators.ValidationException;
import com.ait.lienzo.client.core.types.BoundingBox;
import com.ait.lienzo.client.core.types.NFastDoubleArrayJSO;
import com.ait.lienzo.client.core.types.PathPartList;
import com.ait.lienzo.client.core.types.Point2D;
import com.ait.lienzo.client.core.types.Point2DArray;
import com.ait.lienzo.client.core.util.Geometry;
import com.ait.lienzo.shared.core.types.ShapeType;
import com.google.gwt.json.client.JSONObject;

Expand All @@ -43,6 +46,8 @@ public class PolyLine extends AbstractOffsetMultiPointShape<PolyLine>

private Point2D m_headOffsetPoint = null;

private final PathPartList m_list = new PathPartList();

/**
* Constructor. Creates an instance of a polyline.
*
Expand Down Expand Up @@ -72,16 +77,6 @@ protected PolyLine(final JSONObject node, final ValidationContext ctx) throws Va
refresh();
}

@Override
public PolyLine refresh()
{
if (m_tailOffsetValue != m_headOffsetValue)
{
return this;
}
return this;
}

@Override
public BoundingBox getBoundingBox()
{
Expand All @@ -95,6 +90,24 @@ public BoundingBox getBoundingBox()
*/
@Override
protected boolean prepare(final Context2D context, final Attributes attr, final double alpha)
{
if (m_list.size() < 1)
{
if (false == parse(attr))
{
return false;
}
}
if (m_list.size() < 1)
{
return false;
}
context.path(m_list);

return true;
}

private boolean parse(Attributes attr)
{
Point2DArray list = attr.getPoints();

Expand All @@ -108,22 +121,40 @@ protected boolean prepare(final Context2D context, final Attributes attr, final
{
Point2D point = list.get(0);

context.beginPath();
m_list.M(point.getX(), point.getY());

context.moveTo(point.getX(), point.getY());

for (int i = 1; i < size; i++)
if ( getCornerRadius() == 0 )
{
point = list.get(i);
for (int i = 1; i < size; i++)
{
point = list.get(i);

context.lineTo(point.getX(), point.getY());
m_list.L(point.getX(), point.getY());
}
}
else
{
Geometry.drawArcJoinedLines(m_list, list, getCornerRadius());
}
return true;
}
}
return false;
}

@Override
public PolyLine refresh()
{
m_list.clear();

if (m_tailOffsetValue != m_headOffsetValue)
{
return this;
}
return this;
}


@Override
protected void fill(Context2D context, Attributes attr, double alpha)
{
Expand All @@ -150,6 +181,18 @@ public PolyLine setPoints(final Point2DArray points)
return refresh();
}

public double getCornerRadius()
{
return getAttributes().getCornerRadius();
}

public PolyLine setCornerRadius(final double radius)
{
getAttributes().setCornerRadius(radius);

return refresh();
}

@Override
public PolyLine setPoint2DArray(Point2DArray points)
{
Expand Down Expand Up @@ -187,6 +230,8 @@ public PolyLineFactory()
super(ShapeType.POLYLINE);

addAttribute(Attribute.POINTS, true);

addAttribute(Attribute.CORNER_RADIUS);
}

@Override
Expand Down

0 comments on commit 801cdf8

Please sign in to comment.