This Question is Answered

15 "helpful" answers available (3 pts)
2 Replies Last post: Jul 28, 2008 8:51 PM by RMH

The Scope of GRect?

Jul 28, 2008 1:04 PM

Click to view RMH's profile Curl RMH 45 posts since
Feb 17, 2008
I've been playing around with Shapes and I've noticed that the GRect doesn't seem to behave the way I thought it should. For example if I create a EllipseShape and add it to a canvas I can specify the positioning of the ellipse when I create the Ellipse vis the GRect (I also can use the translation attribute). I would have thought that the GRect of a shape is aligned with its parent so if

{ canvas.add sphere1}
{sphere1.add spehre2}
{sphere2.add sphere3} 

Then sphere3's GRect should be relative to sphere2, but its not. It's stays relative to sphere1 regardless of how deep in the object graphy I go. I don't know if that makes sense but here is my applicaitons I'm working on which I'm trying to make behave like this example written in the processing.org langauge.
{curl 6.0 applet}
 
{import * from CURL.GUI.SHAPES}
{def ran = {Random}}
{def colour = {Color 0,0,0,opacity=0.0}}
    
{define-class public SmokeRing {inherits EllipseShape}
 
  {constructor public {default ...}
    {construct-super.EllipseShape ...}
    {Timer
        interval = ({ran.next-in-range 1,20}/10) * 1s,
        repeat = 1,
        {on TimerEvent do
            def lrOffset = 1mm ||{ran.next-in-range 1,10} *1mm
            def wOffset = 1mm  ||{ran.next-in-range 1,10} *1mm
 
            def childRing =
                {SmokeRing
                    {GRect (5mm + lrOffset), (5mm - lrOffset), (5mm + wOffset), (5mm - wOffset)},
                    color= colour,
                    border-color="black",
                    border-width=1pt
                }
            {self.add childRing}
        }
    }
  }
}
 
 
{value
    let anchorRing:SmokeRing = {SmokeRing {GRect 5mm, 5mm, 5mm, 5mm}, color="red",translation = {Distance2d 20mm,20mm}}
    
    def canvas = {Canvas width=100mm, height=100mm, background="silver"}
 
    {canvas.add anchorRing}
    
    canvas
}
   
Click to view Damian's profile Curl Damian 5 posts since
Nov 7, 2007
1. Re: The Scope of GRect? Jul 28, 2008 3:05 PM

First, a terminology issue: GRects are never relative to anything on their own -- they're just a class that contains four numbers, but they don't mean anything until you give them a context. So, I generally prefer to say "Ellipse2 is relative to Ellipse1," not that their GRects are relative.

Okay. The main confusion is one of origin. Each Shape contains a full 2D transformation matrix that defines its position relative to its parent. This can include scaling, rotation, and translation (position) information. By default, it is the identity matrix, which means that the Shape's coordinate system is no different from that of its parent.

When you specify spatial data for a Shape (for instance, by giving it a GRect to define itself), this data is in that coordinate system -- the Shape's own coordinate system. Giving a Shape a GRect doesn't move its origin. So, this code:

{EllipseShape {GRect -2cm, 3cm, 0cm, 1cm}}

Defines a 1cm x 1cm ellipse whose origin & coordinate system is the same as its parent, and the actual drawing occurs from (2cm, 0cm) to (3cm, 1cm) in its coordinate space. If you then put another Ellipse under it, it still gets the same coordinate space! The GRect doesn't affect it.

Instead of putting the GRect off to the side like above, you want to translate the origin of the Shape. For instance, you might achieve the same result as the above example this way:

{EllipseShape {GRect .5cm, .5cm, .5cm, .5cm}, translation = {Distance2d 2.5cm, 0.5cm}}.


This way the Shape's origin is neatly right in the middle of the 1cm x 1cm ellipse, and we move the whole coordinate system. Any children will then be relative to this point. You can give them their own translation, relative to that point, using the same technique. You can later change that by calling apply-translation or set-translation-in-parent. The first is relative to previous operations; the second is absolute.

Click to view RMH's profile Curl RMH 45 posts since
Feb 17, 2008
2. Re: The Scope of GRect? Jul 28, 2008 8:51 PM
in response to: Damian
Wow! That makes so much sense. Thank you for explaining it so clearly!