uses double dispatch for vector transformation

This commit is contained in:
hneemann 2018-03-18 23:34:28 +01:00
parent a0ca68d29f
commit c845be7976
7 changed files with 16 additions and 25 deletions

View File

@ -200,7 +200,7 @@ public class Polygon implements Iterable<VectorInterface> {
public Polygon transform(Transform transform) {
Polygon p = new Polygon(closed);
for (VectorInterface v : points)
p.add(transform.transform(v));
p.add(v.transform(transform));
p.isBezierStart.addAll(isBezierStart);
return p;
}

View File

@ -8,7 +8,7 @@ package de.neemann.digital.draw.graphics;
/**
* A simple transformation to a given vector
*/
public abstract class Transform {
public interface Transform {
/**
* Transforms an integer vector
@ -16,7 +16,7 @@ public abstract class Transform {
* @param v the vector to transform
* @return the transformed vector
*/
public abstract Vector transform(Vector v);
Vector transform(Vector v);
/**
* Transforms an float vector
@ -24,19 +24,6 @@ public abstract class Transform {
* @param v the vector to transform
* @return the transformed vector
*/
public abstract VectorFloat transform(VectorFloat v);
/**
* Transforms an vector interface
*
* @param v the vector to transform
* @return the transformed vector
*/
public VectorInterface transform(VectorInterface v) {
if (v instanceof Vector)
return transform((Vector) v);
else
return transform(v.getVectorFloat());
}
VectorFloat transform(VectorFloat v);
}

View File

@ -8,7 +8,7 @@ package de.neemann.digital.draw.graphics;
/**
* Implements a rotation and translation.
*/
public class TransformRotate extends Transform {
public class TransformRotate implements Transform {
private final int sin;
private final int cos;

View File

@ -8,7 +8,7 @@ package de.neemann.digital.draw.graphics;
/**
* A translation
*/
public class TransformTranslate extends Transform {
public class TransformTranslate implements Transform {
private final VectorInterface trans;
/**

View File

@ -245,7 +245,7 @@ public class Vector implements VectorInterface {
}
@Override
public VectorFloat getVectorFloat() {
return new VectorFloat(x, y);
public VectorInterface transform(Transform tr) {
return tr.transform(this);
}
}

View File

@ -47,8 +47,8 @@ public class VectorFloat implements VectorInterface {
}
@Override
public VectorFloat getVectorFloat() {
return this;
public VectorInterface transform(Transform tr) {
return tr.transform(this);
}
/**

View File

@ -31,7 +31,11 @@ public interface VectorInterface {
float getYFloat();
/**
* @return this vector as a {@link VectorFloat} instance
* Transforms the vector with the given transformation.
*
* @param tr the transformation to use
* @return the transformed vector
*/
VectorFloat getVectorFloat();
VectorInterface transform(Transform tr);
}