2021-01-31

How to have a class with an instance of its predecessor and continue its heirarchy for all new instances?

I'll try to be as specific as possible so it's easy to understand.

Let's say you have a class that holds a rectangle in the form of (x, y, w, h) when working on a 2d plane with (0,0) being the top left of the display. The first instance of this class holds the values (0,0,1920,1080), which sets the display dimensions for a program.

Now, from here, I want to have all the next instances of Rectangle to exist within the previous one, such that their coordinates act as relative positions to it.

For example, let's work with the next 2 Rectangle instances. I'll declare one called rect1 to have the values (100,100,500,500), and then the second instance called rect2 to exist within rect1. Its values will be (50,50,50,50).

Now, I want the heirarchy of values to apply to all instances to allow the relative positioning. What this would mean is that (relative to the original display's rectangle's first instance), rect1 will have a relative position in the form (x,y) of (100,100), however rect2 will have a relative position of (150,150) TO THE ORIGINAL DISPLAY'S RECTANGLE's FIRST INSTANCE.

But, to make life easy and follow the heirarchy, rect2 will have a RELATIVE POSITION TO rect1 of (50,50), yet in terms of the predasessor of rect1, its relative position is (150,150), as explained before.

Here is a picture of what I am trying to demonstrate:

here

So, with that type of structure in mind, how would I be able to translate that concept into code that follows the same idea?



from Recent Questions - Stack Overflow https://ift.tt/3oAfPg0
https://ift.tt/3j3Zm2H

No comments:

Post a Comment