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.

I will leave the rest up to you.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);

Carlos, you're a genius. Works like a charm. Thanks!

views:1407 update:2011/10/11 15:24:38
corona forums © 2003-2011