## My Stupid code 1

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: Klaus, FourthWorld, heatherlaine, kevinmiller

MaxV
Posts: 1547
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

### My Stupid code 1

Hello,
I need to center an image (presentazione2) inside another image (presentazione), and then shrink proportionally the second in order to fit as much possible inside the first.
This is my stupid code that doesn't use math, do you have a better one?

Code: Select all

``````lock screen
put true into ttt
repeat while ttt
set the width of image "presentazione2" to (the width of image "presentazione2" - 1)
set the height of image "presentazione2" to (the height of image "presentazione2" - 1)
set the loc of image "presentazione2" to the loc of image "presentazione"
if within(image "presentazione", the topleft of image "presentazione2") and within(image "presentazione", the bottomright of image "presentazione2") then
put false into ttt
end if
end repeat
unlock screen
``````
My code is stupid because it wastes a lot of cycles...
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

ClipArtGuy
Posts: 206
Joined: Wed Aug 19, 2015 4:29 pm

### Re: My Stupid code 1

If the size of image "presentazione" is static, you could use something like this handler:

Code: Select all

``````command setAreaProportional pImageID, pWidth, pHeight
local tProportionalWidth, tProportionalHeight, tRatio
put (the formattedWidth of pImageID) / pWidth into tRatio
put (the formattedHeight of pImageID) / tRatio into tProportionalHeight
put (the formattedHeight of pImageID) / pHeight into tRatio
put (the formattedWidth of pImageID) / tRatio into tProportionalWidth
if tProportionalHeight > pHeight then
lock screen
set the width of pImageID to tProportionalWidth
set the height of pImageID to pHeight
unlock screen
else
lock screen
set the width of pImageID to pWidth
set the height of pImageID to tProportionalHeight
unlock screen
end if
end setAreaProportional``````

from the last part of this lesson : http://lessons.livecode.com/m/4071/l/15 ... e-an-image

richmond62
Livecode Opensource Backer
Posts: 2792
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

### Re: My Stupid code 1

shrink proportionally the second in order to fit as much possible inside the first
Do you want the second to be the same size as the first one or smaller?

Anyway, although the code offerings before this are very clever, in my opinion
they are a bit over-complicated.

https://www.dropbox.com/s/f3ud9qhi7qox0 ... e.zip?dl=0

Code: Select all

``````on mouseUp
put the width of img "presentazione" into WIDD
put the height of img "presentazione" into HITE
put (WIDD*0.8) into nWIDD
put (HITE*0.8) into nHITE
set the width of img "presentazione2" to nWIDD
set the height of img "presentazione2" to nHITE
set the loc of img "presentazione2" to the loc of img "presentazione"
end mouseUp
``````

ClipArtGuy
Posts: 206
Joined: Wed Aug 19, 2015 4:29 pm

### Re: My Stupid code 1

That would work, but it doesn't resize proportionally, which I think was part of what MaxV was looking for..

edit - I tweaked the script I originally posted, and it seems to work pretty well. I'm not sure about cycles, but this is working well here:

Code: Select all

``````   local tProportionalWidth, tProportionalHeight, tRatio
put (the formattedWidth of img "presentazione2") / the width of img "presentazione" into tRatio
put (the formattedHeight of img "presentazione2") / tRatio into tProportionalHeight
put (the formattedHeight of img "presentazione2") /the height of img "presentazione" into tRatio
put (the formattedWidth of img "presentazione2") / tRatio into tProportionalWidth
if tProportionalHeight > the height of img "presentazione" then
lock screen
set the width of img "presentazione2" to tProportionalWidth
set the height of img "presentazione2" to the height of img "presentazione"
unlock screen
else
lock screen
set the width of img "presentazione2" to the width of img "presentazione"
set the height of img "presentazione2" to tProportionalHeight
unlock screen
end if
set the loc of img "presentazione2" to the loc of img "presentazione"
``````
edit2 - Just for fun a rudimentary test of speed https://www.dropbox.com/s/c25y2xddzd1n4 ... ecode?dl=0

MaxV
Posts: 1547
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

### Re: My Stupid code 1

ClipArtGuy wrote:
Mon May 07, 2018 9:28 pm

edit - I tweaked the script I originally posted, and it seems to work pretty well. I'm not sure about cycles, but this is working well here:

edit2 - Just for fun a rudimentary test of speed https://www.dropbox.com/s/c25y2xddzd1n4 ... ecode?dl=0
Capture.JPG
Great job I'm using it now and works great!!!!
Thank you!
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w