mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-18 01:14:42 -04:00
uses double dispatch for vector transformation
This commit is contained in:
parent
a0ca68d29f
commit
c845be7976
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -47,8 +47,8 @@ public class VectorFloat implements VectorInterface {
|
||||
}
|
||||
|
||||
@Override
|
||||
public VectorFloat getVectorFloat() {
|
||||
return this;
|
||||
public VectorInterface transform(Transform tr) {
|
||||
return tr.transform(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user