Geometry classes

I would discuss about a small geometry implementation:

Consider having all basic classes like Point, Dimension, Rectangle, Circle.

By example, you will find on a Circle a method like :

public Point getCenter();

What would be your options ?:

  • Having “Point” immutable that let you the liberty to return the Point from the Circle object without any risk that somebody change it.
  • Having “Point” mutable that you must enforce in a some way any modifications (like with listeners). But even if you are able to intercept changes, theses changes may corrupt some objects states. (exemple changing corners of a rectangle that do not give a rectangle anymore).

At this time, my own geometry approche it to use immutable object for all objects. But it’s not the best approche for reduce objects allocations. Each time i want to translate, rotate, … a geometry objects, i must instanciate a new one…

The mutable approches is fine when you perform an animation and translate a series of geometry objects each update() or render() depending of what you do.

Sébastien.

All of those things are simple enough to work with using primitives. Don’t use extra objects for them if you don’t have to.

Immutable objects absolutely easier to reason about, no question. And on the average desktop app, you’re not likely to notice the difference if you create them by the millions. But if you’re creating them every frame on an Android machine, then you end up having to compromise and use mutable objects.

Seems to me modern platforms are pushing us backward :frowning:

I think I came up with a good solution for this mutable immutable problem.

just create an interface for all the immutable methods of a primitive and only have mutable stuff in your implementation.

interface ImmutableVector2{
  float getX();
  float getY();
  float dot(ImmutableVector2
}
class Vector2 implements ImmutableVector2
{
  public float x,y;
  void mul(float a)
  {
     x *= a; y*=a;
  }
}

so now when you want to have a method which returns a vector, but you don’T want that others can modify it just return a immutable one.


class Circle
{
  private final Vector2 center;

  public Circle(Vector2 center)
  {
    this.center = center.clone();
  }

  public ImmutableVector2 getCenter(){return center;}
}

Immutable means “never changes”. It does not mean read only. An type cannot mutable and immutable. A mutable class therefore cannot implement an interface that marks a class as being immutable.

its a practical solution to enforce some constraints, so name it whatever you like :cranky:

No its not. An real immutable class would be practical. This class is broken. Immutability is a different (more practical) restraint than read only access. Name it ReadableVector if you use it, but it’s still true that mutability and immutability or mutually exclusive.

I agree with you, the immutable approches is the more straightforward but not the most efficient in mobile platform.
I don’t like primitives for storing theses kind of coordinates. when i saw a method like xxx(int x1,int y1, int x2 , int y2 , int x3 , int y3, int x4 , int y4 , …) i have a headhache…