summaryrefslogtreecommitdiff
path: root/CSMath/src/bezier/geom/transform/TDHScale.java
blob: c064f1bd45e17c34cfe4b6b65f697ab17eba09ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package bezier.geom.transform;

import bezier.geom.TDHPoint;

/**
 * Transform that does coordinate scaling.
 * 
 * @author bjculkin
 *
 */
public class TDHScale implements TDHTransform {
	/**
	 * Amount to scale x by.
	 */
	public final double sx;
	/**
	 * Amount to scale y by.
	 */
	public final double sy;
	/**
	 * Amount to scale the homogeneous coordinate by.
	 */
	public final double sz;

	/**
	 * Create a new scaling that scales each coordinate by an equal amount
	 * 
	 * @param factor
	 *            The amount to scale the coordinates by.
	 */
	public TDHScale(double factor) {
		this(factor, factor);
	}

	/**
	 * Create a new scaling that scales each coordinate separately.
	 * 
	 * @param sx
	 *            The amount to scale x by.
	 * @param sy
	 *            The amount to scale y by.
	 */
	public TDHScale(double sx, double sy) {
		this(sx, sy, 1);
	}

	/**
	 * Create a new scaling that scales each coordinate separately.
	 * 
	 * Includes the homogeneous coordinate.
	 * 
	 * @param sx
	 *            The amount to scale x by.
	 * @param sy
	 *            The amount to scale y by.
	 * @param sz
	 *            The amount to scale the homogeneous coordinate by.
	 */
	public TDHScale(double sx, double sy, double sz) {
		this.sx = sx;
		this.sy = sy;
		this.sz = sz;
	}

	@Override
	public TDHTransformType type() {
		return TDHTransformType.SCALE;
	}

	@Override
	public TDHPoint transform(TDHPoint punkt) {
		double x = punkt.x * sx;
		double y = punkt.y * sy;
		double z = punkt.z * sz;

		return new TDHPoint(x, y, z);
	}

	@Override
	public double[][] matrix() {
		return new double[][] { new double[] { sx, 0, 0 }, new double[] { 0, sy, 0 }, new double[] { 0, 0, sz } };
	}

	@Override
	public TDHTransform invert() {
		return new TDHScale(1 / sx, 1 / sy, 1 / sy);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(sx);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(sy);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(sz);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TDHScale other = (TDHScale) obj;
		if (Double.doubleToLongBits(sx) != Double.doubleToLongBits(other.sx))
			return false;
		if (Double.doubleToLongBits(sy) != Double.doubleToLongBits(other.sy))
			return false;
		if (Double.doubleToLongBits(sz) != Double.doubleToLongBits(other.sz))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "TDHScale [sx=" + sx + ", sy=" + sy + ", sz=" + sz + "]";
	}
}