Page 1 of 1
My Stupid code 1
Posted: Mon May 07, 2018 6:04 pm
by MaxV
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...
Re: My Stupid code 1
Posted: Mon May 07, 2018 6:59 pm
by ClipArtGuy
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
Re: My Stupid code 1
Posted: Mon May 07, 2018 8:21 pm
by richmond62
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
Re: My Stupid code 1
Posted: Mon May 07, 2018 9:28 pm
by ClipArtGuy
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
Re: My Stupid code 1
Posted: Tue May 08, 2018 4:52 pm
by MaxV
Great job I'm using it now and works great!!!!
Thank you!