Matthias Ronge
Matthias Ronge

Reputation: 10112

How to get the dimension between two points in Java

Given two Points, is there a more native solution to get the area between these, than this?

new Dimension(one.x - other.x, one.y - other.y);

What I think of is something like

Dimension a = new Dimension(one, other);
Dimension b = one.towards(other);

Upvotes: 0

Views: 58

Answers (1)

Matthias Ronge
Matthias Ronge

Reputation: 10112

The syntax is a bit odd, but here it is:

static Dimension sizeBetween(Point from, Point to) {
    Rectangle box = new Rectangle(from);
    box.add(to);
    return box.getSize();
}

Upvotes: 1

Related Questions