that is along the line (xres/2,yres/2) to (0,yres)
The gradient of it's line of motion is (dy by dx), i.e. (yres/2-yres)/(0-xres/2)
The normal to a line of gradient dy/dx is -dy/dx.
That is in 2D co-ordinate geometry, normal= -1/gradient.
The length of the side is 2* .5*SmallRectangleHeight*normalize(normal)
Because the normalize(normal) has a length of one unit.
Here's a slight improvement, by making sure that the bottom point is lifted into the screen fully by a do until loop.
Code: Select all
'beeper
'extern "windows" lib "user32"
declare function _Beep alias "MessageBeep" (byval as integer) as integer
'end extern
Type V2D
As Single x,y
End Type
#define vct Type<V2D>
Function Rotate2D(pivot As V2d,p As V2d,a As Single) As V2D
var rotx=(Cos(a*.0174533)*(p.x-pivot.x)-Sin(a*.0174533)*(p.y-pivot.y))+pivot.x
var roty=(Sin(a*.0174533)*(p.x-pivot.x)+Cos(a*.0174533)*(p.y-pivot.y))+pivot.y
Return vct(rotx,roty)
End Function
Operator + (v1 As V2D,v2 As V2D) As V2D
Return vct(v1.x+v2.x,v1.y+v2.y)
End Operator
Operator -(v1 As V2D,v2 As V2D) As V2D
Return vct(v1.x-v2.x,v1.y-v2.y)
End Operator
Operator * (f As Single,v1 As V2D) As V2D 'scalar*V2D
Return vct(f*v1.x,f*v1.y)
End Operator
Function length(v As V2D) As Single
Return Sqr(v.x*v.x+v.y*v.y)
End Function
Function normalize(v As V2D) As V2D
Dim n As Single=length(v)
If n=0 Then n=1e-20
Return vct(v.x/n,v.y/n)
End Function
Dim As Integer xres,yres
'====================================================
'INPUTS
Dim As Single SmallRectangleHeight=300
Dim As Single BigRectangleWidth=800
Dim As Single BigRectangleHeight=600
Dim As Single rotateangle=.01 'degrees(tweakable)
'=================================================
If BigRectangleHeight>BigRectangleWidth Then Swap BigRectangleHeight,BigRectangleWidth
Screenres BigRectangleWidth,BigRectangleHeight
Screeninfo xres,yres
Dim As V2D ctr=vct(xres/2,yres/2)'screen centre and centres of rectangles
Dim As Single ds,diffx,diffy,inc
Dim As V2D start,norm,temp,temp2,temp3
Dim As Integer flag
start=vct(0,yres)-ctr
#macro motion(inc)
cls
ds=ds+inc 'motion increments
'(dy/dx)= a gradient, -(dx/dy)= a normal to a gradient
'i/e. swap start.x,start.y for (dx/dy), then negate.
norm=vct(-start.y,start.x) 'get the normal vector to the direction of travel
norm=.5*SmallRectangleHeight*normalize(norm)'make the normal vector size= .5*SmallRectangleHeight
temp=ctr+ds*start 'move the SmallRectangle centre onwards
temp2=temp+norm 'top point-red dot
temp3=temp-norm 'bottom point-red dot
Line(temp3.x,temp3.y)-(temp2.x,temp2.y)'join top to bottom points
Circle(temp2.x,temp2.y),3,4,,,,f 'then circle them for the dots
Circle(temp3.x,temp3.y),3,4,,,,f
#endmacro
Do
Screenlock
motion(.001)
'Finishing condition
If temp2.x<=0 Then 'if top point is on the big rectangle(side) i.e done
diffy=yres-temp2.y:diffx= temp3.x
'finish off the other rectangle sides
Line(xres-diffx,0)-(xres,diffy)
Line(temp3.x,yres)-(xres,diffy)
Line(xres-diffx,0)-(temp2.x,temp2.y)
Screenunlock:_Beep(-1):Exit Do
End If
If temp3.y>=yres Then 'if bottom point is on the big rectangle (base)
If temp2.x>0 Then 'if top point is still in the big rectangle (screen)
do
start=rotate2D(ctr,start,rotateangle)'lift the bottom point up into the screen
'no forwaed motion while lifting
motion(0)
loop until temp3.y<yres
End If
End If
Screenunlock
Sleep 1,1
If Len(Inkey) Then flag=1: Exit Do
Loop
If flag=0 Then
Print "Length of inside rectangle = ";length(vct(temp3.x,yres)-vct(xres,diffy))
Print "Height of inside rectangle = ";length(temp2-temp3)
Else
Print "Exit"
End If
Sleep