Unexpected result with Set Points & Set Rectangle

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
KimD
Posts: 225
Joined: Wed Jul 08, 2015 5:51 am

Unexpected result with Set Points & Set Rectangle

Post by KimD » Tue Oct 27, 2015 10:11 pm

Hi

I'm getting a result that I don't expect when setting the points of a graphic and setting the rectangle of the same graphic.

My stack is as follows -

Stack

Card1

On PreOpenCard
Set the rectangle of graphic "ABC" to 0,0,200,200
Put Empty into field "DisplayPoints"
End PreOpenCard

Graphic "ABC" // Type = line, and EMPTY OF POINTS ON STARTUP

Button1
on mouseUp
set the points of graphic "ABC" to the points of graphic "ABC" & cr & 100 & comma & 100
Set the rectangle of graphic "ABC" to 0,0,200,200 // same as on the PreOpenCard
Put the points of graphic "ABC" into field "DisplayPoints"
end mouseUp

Field DisplayPoints // Only purpose is to list the points of graphic ABC (so that we can see what is happening)

Note that the points that I add and the the set rectangle instruction are both constant.

I'm expecting that after three presses of Button 1, I will see the following in field DisplayPoints
100,100
100,100
100,100
Growing by another "100,100" with each push of the button.

But instead what I'm getting is:
1) An initial 100,100 (all good so far)
2) Which on the second press of the button gets overwritten by
1,1
199,199
3) And after that I get a 100,100 added with each push of the button.
4) So that after 10 pushes of the button I've got
1,1
199,199
100,100
100,100
100,100
100,100
100,100
100,100
100,100
100,100

I don't understand why I'm getting those first two values, when both the points that I'm adding and the "Set Rectangle" that I'm using never change.

Any advice would be much appreciated.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Unexpected result with Set Points & Set Rectangle

Post by bn » Wed Oct 28, 2015 12:04 am

Hi Kim,

What you see is what you have coded.

I try to explain.

When you have a line graphic it is made up of two points. These two points define the line. Now you set the rect of that line-graphic to 0,0,200,200 in your preopenCard script. Now the 2 points that define the line-graphic are automatically changed to the location on the card and you see 1,199. This is because all point defined graphics in LC are "inside" the line size. If you would set with the same script as in your preopenCard handler grc "ABC" and you would set the lineSize (= border width) to 0 you would get the points 0,200. However you would not see any lines because the linesize is 0.

The main thing to understand here is that the points of a graphic are exactly the points on the card. That means if you set the points of a point based graphic either manually or by script in its dimensions the points that make up the graphic are changed/transformed to their new locations.

Now you add a new point 100,100 to your line graphic which is now not a line graphic but a polygon graphic made up of mulitple points.
This is how you get
1,1
199,199
100,100

The appearence of the graphic will not change although you set the rectangle, but it is the same as before. The first two lines are the same because the rectangle did not change and the third point actually draws a line from point
199,199 to 100,100. But you don't see that line since it is halfway back up on the way to 1,1.

Now you repeat adding a point 100,100 and that does not change the appearence either because all the 100,100 points are drawn on top of each other.

The graphics that are point-based in LC are Line, Freehand and freehandPolygon. The lower row for graphics in the tool palette.
The upper row are also graphics but not point base but those are drawn by the operating system: the Rectangle, RoundedRect, Oval and Multiside. Those are defined by their rect (width and height).
The point based graphics define the rect they make by the value of the points and when changing the rectangle of those you implicitly force a recalculation of the points that make up that graphic.
You can easily see that in the inspector for these after moving the graphic around (which also changed the points) or the width and or height.

I hope what I wrote makes any sense in explaining why what you see is exactly what you have coded.
If anything is not clear please feel free to ask.

Kind regards

Bernd

KimD
Posts: 225
Joined: Wed Jul 08, 2015 5:51 am

Re: Unexpected result with Set Points & Set Rectangle

Post by KimD » Wed Oct 28, 2015 12:40 am

Hi Bernd

Thanks for the info. I was (slowly) getting there as a result of my own experimentation.

If I understand what you are saying:
1) The boundaries of a point-based graphic are the SMALLEST rectangle that can accommodate its component points; and
2) The relative location of the component points within a point-based graphic are dependent on its boundaries; and
3) If you change either the boundaries, or the component points, then the other will AUTOMATICALLY update.

Is that correct?

If "Yes" then damn! As I was hoping to be able to create a point-based graphic that had a bounding rectangle that was LARGER THAN ITS COMPONENT POINTS, then write additional points inside that space.

Regards

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: Unexpected result with Set Points & Set Rectangle

Post by bn » Wed Oct 28, 2015 12:51 am

Hi Kim,

that is all correct.
As I was hoping to be able to create a point-based graphic that had a bounding rectangle that was LARGER THAN ITS COMPONENT POINTS, then write additional points inside that space.


Why don't you just add points.

If you mean to have some sorts of "subGraphics" then you can do that by separarating the "subGraphics" by an empty line

Code: Select all

on mouseUp
   set the points of grc "ABC" to 10,10 & cr & 20,10 & cr & 20,20 & cr & 10,20 & cr & 10,10
   put the points of grc "ABC" into tPoints
   put cr & cr & 40,40 & cr & 60,40 & cr & 60,60 & cr & 40,60 & cr & 40,40 after tPoints
   set the points of grc "ABC" to tPoints
end mouseUp
If that is what you want. It is still one graphic consisting of 2 "sub graphics", very powerful when working with graphics.

Kind regards
Bernd

KimD
Posts: 225
Joined: Wed Jul 08, 2015 5:51 am

Re: Unexpected result with Set Points & Set Rectangle

Post by KimD » Wed Oct 28, 2015 1:35 am

Brilliant. I put a single point, separated by two CRs, at each of the outer corners of my point-based graphic canvas - and now it works exactly as I had hoped.

A big thanks for your input. I wish that I'd asked you two days ago!

Regards

Post Reply