Depends on how you want to square to be drawn. A horizontal line could be either the top or the bottom of the square...
If you are drawing orthogonal lines (horizontal or vertical), the answer will be pretty simple.
Take the points of the line, work out how far apart they are, and create a second set of points offset in the other direction that same distance.
i.e. horizontal line 0,0 ; 100,0 (i.e. x1,y1 ; x2,y2)
100-0 = 100,
create points 0,100 ; 100,100
use the start set of points 0,0 ; 100,0 and the end 100,0 ; 100,100 and draw a rectangle (in this instance it will end up as a square) with those points
To code, you would either need to limit movement only in the x direction, or limit it in the x or y and work out which direction you went (if x1-x2 is not 0 then... else if y1-y2 is not 0 then...)
If you go off orthogonal, into oblique lines, you'll just need to do maths.
Any line is defined by y=mx+b (m being slope, b being y intercept. To work it out from points...
m= y2-y1/x2-x1
b= the formula when x=0 OR... plug in one set of points, and the abovementioned slope
b= y1-(y2-y1/x2-x1)*x1
Then proceed as above, but work out the opposing oblique slope (original slope = m, perpendicular slope = 1/m) and the distance between the two will be sqrt((x2-x1)^2 + (y2-y1)^2) (i.e. Pythagoras)
(insert super maths here... I won't show my working because it's long winded.)
Code: Select all
--put this in card/stack script
global pDrawRect
on opencard --or openstack--
put "False" into pDrawRect
end opencard --or openstack--
--Draw a rectangle to act as a drawing area, and put this script on it:
global pDrawrect
local tInitialPoints
on mousedown
if there is a grc "Square1" then delete grc "Square1"
if there is a grc "Square2" then delete grc "Square2"
if there is a grc "Square3" then delete grc "Square3"
if there is a grc "Square4" then delete grc "Square4"
if pDrawrect = "False" then
put "true" into pDrawrect
put empty into tInitialPoints
put the mouseloc into tInitialPoints
end if
end mousedown
on mouseup
if pDrawrect = "True" then
put empty into tEndPoints
put empty into tRectPoints
put the mouseloc into tEndPoints
put tInitialPoints & comma & tEndPoints into tRectPoints
set the style of the templateGraphic to "line"
create grc "Square1"
set the points of grc "Square1" to tRectPoints
set the itemdel to comma
put ((item 4 of tRectPoints - item 2 of tRectPoints)/(item 3 of tRectPoints - item 1 of tRectPoints)) into pSlope
put (sqrt((item 3 of tRectPoints- item 1 of tRectPoints)^2+(item 4 of tRectPoints- item 2 of tRectPoints)^2)) into pDistance
put comma & (item 3 of tRectPoints + (sqrt(pDistance^2/(1+((-(1/pSlope))^2))))) after tRectPoints
put comma & (-(1/pSlope)*item 5 of tRectPoints+(item 4 of tRectPoints-(-(1/pslope)*item 3 of tRectPoints))) after tRectPoints
put comma & (item 1 of tRectPoints + (sqrt(pDistance^2/(1+((-(1/pSlope))^2))))) after tRectPoints
put comma & (-(1/pSlope)*item 7 of tRectPoints+(item 2 of tRectPoints-(-(1/pslope)*item 1 of tRectPoints))) after tRectPoints
create grc "Square2"
put item 3 of tRectPoints & comma & item 4 of tRectPoints & comma & item 5 of tRectPoints & comma & item 6 of tRectPoints into tLinePoints2
set the points of grc "Square2" to tLinePoints2
create grc "Square3"
put item 5 of tRectPoints & comma & item 6 of tRectPoints & comma & item 7 of tRectPoints & comma & item 8 of tRectPoints into tLinePoints2
set the points of grc "Square3" to tLinePoints2
create grc "Square4"
put item 7 of tRectPoints & comma & item 8 of tRectPoints & comma & item 1 of tRectPoints & comma & item 2 of tRectPoints into tLinePoints2
set the points of grc "Square4" to tLinePoints2
end if
put "False" into pDrawRect
end mouseup
Save the stack and reopen so the initial true/false statement is placed into the pDrawRect- this will decide whether or not to draw something.
To use, click in a place, hold and drag the mouse to a new location, and a square will be drawn when you release.
There will most likely be an issue with the mouse moving one direction or another. I have code to sort this out, just not handy. You basically add a shuffling code that works out what order to put the points to make sure you don't run into maths issues. You also need to NOT move horizontally or vertically, as this creates a divide by zero error. Again, you can throw in some code to work out whether or not the x / y values are the same and handle those states differently of needs be.
Anyway... I have tried the code and it worked for me...
Hope that helped.
XdM