Beyond Me (Math): Drawing a rectangle with your finger....

So I have this partially working, but I can’t get it to appear in the right place. Basically I’m letting the user draw a square/rectangle by dragging.

This is in the ended phase of the drag event:

recWidth = math.abs(event.x - event.xStart)
recHeight = math.abs(event.x - event.xStart)
local myRectangle = display.newRect(event.xStart, event.yStart, recWidth, recHeight)

and it works in that it does actually render a rectangle. But, it seems to be mostly wrong. I’m sure there is some mathematical equation for this, but I barely graduated from high school and it’s making my brain hurt.

You’ll see in the drawing here where the rectangle should appear:
https://img.skitch.com/20110813-m257gr8qef2n5y93m4gentsxcy.jpg

Any help is GREATLY appreciated.

Thanks!
scott. [import]uid: 19193 topic_id: 13755 reply_id: 313755[/import]

I will leave the rest up to you…

[code]
local topLeft= {}
local rc = nil;
local function onTouch(event)

local phase = event.phase

if phase == ‘began’ then
topLeft.x = event.x
topLeft.y = event.y

elseif ‘moved’ == phase then

if (rc ~= nil ) then
rc:removeSelf()
end

w = event.x - topLeft.x
h = event.y - topLeft.y

rc = display.newRect(topLeft.x,topLeft.y, w, h )
rc:setFillColor(0,0,0,0);
rc.strokeWidth = 2
rc:setStrokeColor(128,128,128)

end
return true
end
Runtime:addEventListener(“touch”,onTouch);
[/code] [import]uid: 24 topic_id: 13755 reply_id: 50513[/import]

Carlos, you’re a genius. Works like a charm. Thanks! [import]uid: 19193 topic_id: 13755 reply_id: 50515[/import]