Environment:
OSX 10.8.2 (Mountain Lion)
LiveCode 5.5.3 (Personal Edition)
I have started to learn LiveCode and written a couple of simple stacks and done a couple of tutorials and I really enjoy the voyage so far.
Problem:
In my current project I'm trying to implement a basic version of the Logo scripting language in LiveCode to get hang of more in-depth textmanipulation and the
drawing capacity of LiveCode. I have implemented commands like forward, rotate left , rotate right, penup,pendown, pencolor, background colors, repeat etc
and it works quite nice sofar even though there is alot to do when it comes down to optimize the code.
The thing I have really trouble with all the time was to implement a routine for drawing a Circle. It has been some hard lessons on the way but also some valueble knowledge on
how things works in LiveCode.
In Logo you can draw a circle using the following command
Code: Select all
repeat 360 [forward 1 right turn 1]
that works fine inside LiveCode IDE. I can draw something that "is-not-a-perfect-circle" but quite near.
However, when I compile my code and run it as a standalone application. The "not-so-perfect circle" is more like a "hexagon" then a circle. I have struggled with this for hours
trying to understand what is going on but now I need to know wether or not I am to blame or, if this is some kind of bug in LiveCode. I think it has to something with how
sin,cos or pi somehow works different after compiling the code.
I have included sample-code below that can be used to reproduce my problem + a image that shows the difference between running the code in IDE and as Standalone Application.
The code to be used to reproduce the behavor:
Code: Select all
#############################################
# This testcase shows a strange behavor
# between drawing in compile-free environment
# and when do the drawing in compiled version
# To use this code you need.
#
# Testcase setup on OSX 10.8.2 using LiveCode version 5.5.3
#
# 1. A mainstack with 1 card using dimensions 400x400
# This code to paste on stack-level
# 2. A button on the bottom of the card called "Start"
# that calls drawCircle
# 3. A button on the bottom of the card called "Clear
# that calls clearScreen
#
# To reproduce the behavor
# 1. Save stack as standalone application
# 2. Run the code inside LiveCode and check the circle
# 3. Start the standalone application side by side of LiveCode
# 4. Again run the code and see the difference on how the circle
# looks like in the compiled product.
#############################################
global cAngle,cLoc
############################################
# Convert a circle degree (angle) to radians
# since LiveCode sin() and cos() requires
# radians rather then degrees.
############################################
function toRadians pAngle
return ((pAngle*pi)/180)
end toRadians
#############################################
# Calculate delta between start and end point
#############################################
function deltaX pDist,pAngle
return (pDist*cos(toRadians(pAngle)))*-1
end deltaX
function deltaY pDist,pAngle
return (pDist*sin(toRadians(pAngle)))
end deltaY
#########################################
# ClearScreen
#########################################
on clearScreen
lock screen
lock messages
repeat until the number of images is 0 on card 1
delete image 1 of card 1
end repeat
unlock messages
unlock screen
end clearScreen
#######################################################
# Rotate grades to "The Unit Circle" where 90 deegre
# is to the north and not to the west. In LiveCode
# we have 0 (360) degrees to the north
#######################################################
function unitCircle p_angle
# works ok
if (p_angle >=0 and p_angle <= 90) then
put p_angle - 90 into c_angle
else
put p_angle - 90 into c_angle
end if
return c_angle
end unitCircle
###########################################
# Print out debuginfo on angle and Location
###########################################
on pDebug
put cAngle into field "Angle" on card 1
put round(word 1 of cLoc) & "," & round(word 2 of cLoc) into field "Location" on card 1
end pDebug
############################################
# Draw information on to canvas
############################################
on drawLine p_cLoc,p_eLoc
set pencolor to "black"
set penheight to 2
set penwidth to 2
put word 1 of p_cLoc into x_start
put word 2 of p_cLoc into y_start
put word 1 of p_eLoc into x_end
put word 2 of p_eLoc into y_end
choose line tool
drag from x_start,y_start to x_end,y_end
choose browse tool
pDebug
end drawLine
########################################
# Turn x degree to the right
########################################
on rightTurn tCommand
if cAngle = 0 then
put 360 into cAngle
end if
put cAngle - word 2 of tCommand into cAngle
pDebug
end rightTurn
############################################
# moveForward calculates start and endpoint
# for drawing.
#############################################
on moveForward tCommand
put word 2 of tCommand into tDistance
-- save current position into variables t_x0,t_y0
put word 1 of cLoc into t_x0
put word 2 of cLoc into t_y0
-- Calculate the delta beteen current and new position
-- Convert the Circle so that 90 deegrees points to the north
put unitCircle (cAngle) into l_Angle
-- Calculate the delta for the unit circle
put deltaX (tDistance,l_Angle) into t_deltax
put deltaY (tDistance,l_Angle) into t_deltay
-- Calculate the new x,y position with help of delta
put t_x0 + t_deltax into t_xpos
put t_y0 + t_deltay into t_ypos
put t_xpos & " " & t_ypos into theNewLoc
-- Call drawline to draw to the new location
drawLine cLoc,theNewLoc
-- Put the new location into current location
put theNewLoc into cLoc
end moveForward
#############################################
# drawCircle : Draws a cirlce on the canvas
# using logocommands forward
# and rotate.
#############################################
on drawCircle
put "199 202" into cLoc
put 360 into cAngle
pDebug
repeat with step= 360 down to 0
put step into cAngle
put "fd 1" into cForward
moveForward cForward
put "rt 1" into cRightTurn
rightTurn cRightTurn
end repeat
end drawCircle
Kindly Rgds
/Ulf