Skip to content

Commit

Permalink
fix polyline
Browse files Browse the repository at this point in the history
  • Loading branch information
earlygrey committed Dec 12, 2022
1 parent 97c28db commit c76bcb5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
19 changes: 10 additions & 9 deletions drawer/src/space/earlygrey/shapedrawer/shapes/BasicLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

import space.earlygrey.shapedrawer.JoinType;
import space.earlygrey.shapedrawer.ShapeDrawer;
import space.earlygrey.shapedrawer.ShapeUtils.LineWidthFunction;
import space.earlygrey.shapedrawer.shapes.Shape.Line;

public class BasicLine extends BasicShape<BasicLine> implements Line {

private Vector2 from, to;
private float width;
private float startColor, endColor;
boolean snap;

Expand All @@ -28,24 +28,27 @@ void reset(boolean filled) {
startColor = drawer.getPackedColor();
endColor = drawer.getPackedColor();
snap = drawer.isDefaultSnap();
width = drawer.getDefaultLineWidth();
}

@Override
public BasicLine from(float x, float y) {
from.set(x, y);
return this;
}

@Override
public BasicLine to(float x, float y) {
to.set(x, y);
return this;
}

@Override
public BasicLine from(Vector2 from) {
this.from.set(from);
return this;
}

@Override
public BasicLine to(Vector2 to) {
this.to.set(to);
return this;
Expand All @@ -63,36 +66,34 @@ public BasicLine lineWidth(float width) {
return this;
}


public BasicLine width(float width) {
this.width = width;
return this;
}

@Override
public BasicLine color(Color color) {
startColor(color);
endColor(color);
return this;
}

@Override
public BasicLine startColor(Color startColor) {
this.startColor = startColor.toFloatBits();
return this;
}

@Override
public BasicLine endColor(Color endColor) {
this.endColor = endColor.toFloatBits();
return this;
}

@Override
public BasicLine snap(boolean snap) {
this.snap = snap;
return this;
}

@Override
public void draw() {
drawer.line(from.x, from.y, to.x, to.y, width, snap, startColor, endColor);
drawer.line(from.x, from.y, to.x, to.y, lineWidth.getWidth(0, 0), snap, startColor, endColor);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import space.earlygrey.shapedrawer.JoinType;
import space.earlygrey.shapedrawer.ShapeDrawer;
import space.earlygrey.shapedrawer.ShapeUtils.LineWidthFunction;
import space.earlygrey.shapedrawer.shapes.Shape.PolyLine;

public class BasicPolygon extends BasicShape<BasicPolygon> implements PolyLine {
public class BasicPolyline extends BasicShape<BasicPolyline> implements PolyLine {

FloatArray vertices;

Expand All @@ -20,7 +21,7 @@ public class BasicPolygon extends BasicShape<BasicPolygon> implements PolyLine {

EarClippingTriangulator triangulator = new EarClippingTriangulator();

BasicPolygon(ShapeDrawer drawer) {
BasicPolyline(ShapeDrawer drawer) {
super(drawer);
vertices = new FloatArray();
}
Expand All @@ -36,70 +37,76 @@ void reset(boolean filled) {
scaleY = 1;
}

public BasicPolygon setOpen(boolean open) {
public BasicPolyline setOpen(boolean open) {
this.open = open;
return this;
}

@Override
public BasicPolygon color(Color color) {
public BasicPolyline color(Color color) {
this.color = color.toFloatBits();
return this;
}

@Override
public BasicPolygon joinType(JoinType joinType) {
public BasicPolyline joinType(JoinType joinType) {
this.joinType = joinType;
return this;
}

@Override
public BasicPolygon lineWidth(float width) {
public BasicPolyline lineWidth(float width) {
setLineWidth(width);
return this;
}

@Override
public <T extends Vector2> BasicPolygon vertices(Iterable<T> points) {
public BasicPolyline lineWidth(LineWidthFunction width) {
this.lineWidth = width;
return this;
}

@Override
public <T extends Vector2> BasicPolyline vertices(Iterable<T> points) {
this.vertices.clear();
points.forEach(p -> this.vertices.add(p.x, p.y));
return this;
}

@Override
public BasicPolygon vertices(FloatArray points) {
public BasicPolyline vertices(FloatArray points) {
this.vertices.clear();
this.vertices.addAll(points);
return this;
}

@Override
public BasicPolygon vertices(float[] points) {
public BasicPolyline vertices(float[] points) {
this.vertices.clear();
this.vertices.addAll(points, 0, points.length);
return this;
}

@Override
public BasicPolygon addVertex(float x, float y) {
public BasicPolyline addVertex(float x, float y) {
this.vertices.add(x, y);
return this;
}

@Override
public BasicPolygon addVertex(Vector2 vertex) {
public BasicPolyline addVertex(Vector2 vertex) {
return addVertex(vertex.x, vertex.y);
}

@Override
public BasicPolygon offset(float x, float y) {
public BasicPolyline offset(float x, float y) {
offsetX = x;
offsetY = y;
return this;
}

@Override
public BasicPolygon scale(float x, float y) {
public BasicPolyline scale(float x, float y) {
scaleX = x;
scaleY = y;
return this;
Expand Down
12 changes: 6 additions & 6 deletions drawer/src/space/earlygrey/shapedrawer/shapes/Pencil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ public Line line() {
return LINE;
}

public PolyLine path() {
POLYGON.reset(false);
POLYGON.setOpen(true);
return POLYGON;
public PolyLine polyLine() {
POLYLINE.reset(false);
POLYLINE.setOpen(true);
return POLYLINE;
}

@Override
public OutlinedPolygon<?> polygon() {
POLYGON.reset(filled());
return POLYGON;
POLYLINE.reset(filled());
return POLYLINE;
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion drawer/src/space/earlygrey/shapedrawer/shapes/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.badlogic.gdx.utils.FloatArray;

import space.earlygrey.shapedrawer.JoinType;
import space.earlygrey.shapedrawer.ShapeUtils.LineWidthFunction;

public interface Shape<T extends Shape> {

Expand Down Expand Up @@ -124,7 +125,7 @@ interface OutlinedPolygon<T extends OutlinedPolygon> extends FilledPolygon<T> {
}

interface PolyLine extends OutlinedPolygon<PolyLine> {

PolyLine lineWidth(LineWidthFunction width);
}

interface FilledRegularPolygon<T extends FilledRegularPolygon> extends Shape<T> {
Expand Down
8 changes: 4 additions & 4 deletions drawer/src/space/earlygrey/shapedrawer/shapes/Utensil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
abstract class Utensil {

ShapeDrawer drawer;
final BasicPolygon POLYGON;
final BasicPolyline POLYLINE;

final BasicRegularPolygon REGULAR_POLYGON;

Expand All @@ -27,7 +27,7 @@ abstract class Utensil {

Utensil(ShapeDrawer drawer) {
this.drawer = drawer;
POLYGON = new BasicPolygon(drawer);
POLYLINE = new BasicPolyline(drawer);
REGULAR_POLYGON = new BasicRegularPolygon(drawer);
CIRCLE = new BasicCircle(drawer);
ELLIPSE = new BasicEllipse(drawer);
Expand All @@ -39,8 +39,8 @@ abstract class Utensil {
abstract boolean filled();

public FilledPolygon polygon() {
POLYGON.reset(filled());
return POLYGON;
POLYLINE.reset(filled());
return POLYLINE;
}

public FilledRegularPolygon regularPolygon() {
Expand Down

0 comments on commit c76bcb5

Please sign in to comment.