Skip to main content
edited title
Link
Tagor
  • 169
  • 4

Abstract away responsibility from caller without introducing additional computational complexity

Source Link
Tagor
  • 169
  • 4

Abstract away responsibility from caller without introducing additional computational complexity

I have two classes:

public class Child {
    public List<Vector2> localPoints;
    public List<Vector2> localEdges;
}

public class Parent {
    public List<Child> children;
    public Vector2 worldPos;
}

A parent has a position in the world worldPos. localPoints and localEdges in child contains points relative to worldPos.

To get the position in the world of a Vector2 in localPoints orlocalEdges the caller can just add worldPos to each element.

      foreach (Parent parent in parents) {
        foreach (Child child in parent.children) {
            foreach (Vector2 point in child.localPoints) {
                Debug.Log(point + parent.worldPos);
            }
            foreach (Vector2 edge in child.localEdges) {
                Debug.Log(edge + parent.worldPos);
            }
        }
    

The issue is that i have more lists and even shapes stored in a child. Giving the caller the responsibility to add worldPos at every usage becomes very cumbersome.

How can i abstract away that offsetting without introducing additional computational complexity?

I have thought about this and found two solutions that abstracts away the offsetting:

  1. Every time worldPos change, go trough the data in each child apply the change. localPoints andlocalEdges would now be the actual positions in the world.

  2. Store worldPos in each child aswell and use a custom getter that creates a copy of the desired list, apply the offset to each element in the list and return it.

Unfortunately both of these introduce computational complexity.

How do i abstract away the offsetting without introducing computaional complexity?