1 /* 2 * Copyright (c) 2018 Helmut Neemann. 3 * Use of this source code is governed by the GPL v3 license 4 * that can be found in the LICENSE file. 5 */ 6 package de.neemann.digital.draw.graphics; 7 8 /** 9 * A translation 10 */ 11 public class TransformTranslate implements Transform { 12 private final VectorInterface trans; 13 14 /** 15 * Creates a new instance 16 * 17 * @param trans the translation 18 */ TransformTranslate(VectorInterface trans)19 public TransformTranslate(VectorInterface trans) { 20 this.trans = trans; 21 } 22 23 /** 24 * Creates a new instance 25 * 26 * @param x the x translation 27 * @param y the y translation 28 */ TransformTranslate(int x, int y)29 public TransformTranslate(int x, int y) { 30 this(new Vector(x, y)); 31 } 32 33 /** 34 * Creates a new instance 35 * 36 * @param x the x translation 37 * @param y the y translation 38 */ TransformTranslate(float x, float y)39 public TransformTranslate(float x, float y) { 40 this(new VectorFloat(x, y)); 41 } 42 43 @Override transform(Vector v)44 public Vector transform(Vector v) { 45 return v.add(trans.getX(), trans.getY()); 46 } 47 48 @Override transform(VectorFloat v)49 public VectorFloat transform(VectorFloat v) { 50 return new VectorFloat(v.getXFloat() + trans.getXFloat(), v.getYFloat() + trans.getYFloat()); 51 } 52 53 @Override getMatrix()54 public TransformMatrix getMatrix() { 55 return new TransformMatrix(1, 0, 0, 1, trans.getXFloat(), trans.getYFloat()); 56 } 57 58 @Override invert()59 public Transform invert() { 60 return new TransformTranslate(trans.div(-1)); 61 } 62 } 63