I will periodically post coding challenges to be solved with LiveCode.
Solving problems is a great way to improve your coding skills.
Vote for the best answer.
please paste plain text only.
please add [code] at the beginning of your code, and [/code] at the end to facilitate syntax colorizing.
if you see that ‘AndyP edited….’ your answer, this will be only for formatting.. I will not be changing your answer.
put “circle1.png” into tName
put the imageData of image tName into tID
put the milliseconds into t
put the width of image tName into tW
put the height of image tName into tH
put tW * tH into tCountPixel
put tCountPixel * 4 into tCountBytes
— get white space above circle in horiz middle of image
put tW * 2 into tHalfRowBytes
add tHalfRowBytes mod 4 to tHalfRowBytes — to fill up to full 4 bytes per pixel
put tW * 4 into tOneRowBytes
put tW*tH*4 into tIDLength
— find first non-white pixel in horizontal middle of image and bail out
repeat with j = tHalfRowBytes to tCountBytes – tHalfRowBytes step tOneRowBytes
if byteToNum (byte j of tID) < 255 then
put j into tHitByte
— tHit is the byte number of the first black pixel, turn this into
— number of rows above black circle = white space above
put tHitByte div tOneRowBytes – 1 into tWhiteSpaceAbove
if tHitByte mod tOneRowBytes > 0 then add 1 to tWhiteSpaceAbove
— tHeightOfWhiteFrame is height of white frame
— above first black pixel in horizontal middle
— assuming that the image is symetrical and non-white (antialiasing)
— and it is truly a circle we now assume we can get
— the true radius of the black circle by subtracting
— tWhiteSpaceAbove from half the height of image
put tH / 2 – tWhiteSpaceAbove into tRadiusInPixel
— count all non-white pixel of imageData
— by testing for byte 2 of each pixel
— which would not be 255 in case of non-white
repeat with o = 2 to tIDLength – 4 step 4
if byteToNum(byte o of tID) < 255 then
add 1 to tCountBlackWhole
put tRadiusInPixel^2 into tSquareRadius
put tCountBlackWhole / tSquareRadius into tAPi — formula from Wikipedia
— another formula to get pi from areas
put (tRadiusInPixel * 2)^2 into tAreaQuadrat
put 4* tCountBlackWhole/tAreaQuadrat into tAnotherPi
put the milliseconds – t into tTIme
put “Circle diameter is: ” & 2*tRadiusInPixel & ” with area = ” & \
tCountBlackWhole & cr & “Qur approx pi = ” & tAPi & cr & \
tTime & ” millisecs” into field “Out”