summaryrefslogtreecommitdiff
path: root/core/src/main/java/coffee/liz/ecs/math/Vec2f.java
blob: 4bd0529e36b947b5fc4d50eb04bfe6adcce1acb3 (plain) (blame)
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
package coffee.liz.ecs.math;

import static java.lang.Math.sqrt;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.function.Function;

/** Float impl of {@link Vec2}. */
@Getter
@RequiredArgsConstructor
@Builder
@EqualsAndHashCode
public final class Vec2f implements Vec2<Float> {
    /** X coordinate. */
    private final Float x;

    /** Y coordinate. */
    private final Float y;

    @Override
    public Vec2<Float> plus(final Vec2<Float> other) {
        return new Vec2f(x + other.getX(), y + other.getY());
    }

    @Override
    public Vec2<Float> minus(final Vec2<Float> other) {
        return new Vec2f(x - other.getX(), y - other.getY());
    }

    @Override
    public Vec2<Float> scale(final Float scaleX, final Float scaleY) {
        return new Vec2f(x * scaleX, y * scaleY);
    }

    @Override
    public float length() {
        return (float) sqrt(x * x + y * y);
    }

    @Override
    public Vec2<Float> floatValue() {
        return this;
    }

    @Override
    public Vec2<Float> transform(final Function<Float, Float> xTransform, final Function<Float, Float> yTransform) {
        return new Vec2f(xTransform.apply(getX()), yTransform.apply(getY()));
    }

    @Override
    public Vec2<Integer> intValue() {
        return Vec2i.builder().x(this.x.intValue()).y(this.y.intValue()).build();
    }

    /** Zero float vec */
    public static Vec2<Float> ZERO = Vec2f.builder().x(0f).y(0f).build();
}