April 29, 2024, 09:05:36 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


How to Draw Rectangles at an Angle

Started by yujinwunz, December 27, 2008, 08:13:24 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

yujinwunz

Hi everyone. i cant find it in the user guide but i want to know how to draw rectangles at an angle.

any response is appriciated.

-Yujin

Ionic Wind Support Team

Many ways.  Lets see...

-You could use the API function SetWorldTransform to rotate any drawing element, but its a little complicated, and kind of an overkill to rotate a simple rectangle.

-You could do it yourself, using cos and sin, and drawing individual lines.

-You could use GDI+ and rotate the rectangle as an image

-You could use the 2D commands, draw a rectangle to a sprite, and rotate it.

I prefer to use math to solve problems, so here is a very old example that I wrote eons ago that can rotate any n-sided polygon, including rectangles.


/*
EBASIC example program
Draws N sided polygons in a DirectX window
Requires DirectX 7.0 or greater installed

Compile as a WINDOWS target
*/

DEF w as WINDOW
DEF viewangle as DOUBLE
viewangle = 0
OPENWINDOW w,0,0,600,400,@NOAUTODRAW,0,"Polygons",&linehandler
ATTACHSCREEN w,600,400
STARTTIMER w,125


WAITUNTIL w = NULL

END

SUB linehandler
SELECT @MESSAGE
CASE @IDCLOSEWINDOW
CLOSESCREEN
CLOSEWINDOW w
CASE @IDTIMER
FILLSCREEN 0xFFFFFF
viewangle = viewangle + 10
IF viewangle >= 360 THEN viewangle = 0
FOR x = 4 to 8
MOVE BackBuffer,70+(x-4)*102,120
PRINT BackBuffer,x
'Draw3DNPolygon(BackBuffer,x,70+(x-4)*102,130,100,0,RGB(192,192,192),2,viewangle)
DrawNPolygon(BackBuffer,x,70+(x-4)*102,130,100,RGB(0,0,255),2,viewangle)
NEXT x
FOR x = 9 to 13
MOVE BackBuffer,70+(x-9)*102,230
PRINT BackBuffer,x
Draw3DNPolygon(BackBuffer,x,70+(x-9)*102,240,100,0,RGB(192,192,192),2,viewangle)
NEXT x
FLIP
ENDSELECT
RETURN
ENDSUB

SUB DrawNPolygon(win as WINDOW,sides as int,x as INT,y as INT,diameter as DOUBLE,clr as UINT,OPT linewidth=1 AS INT,opt rotation = 0.0 as DOUBLE)
'draws a regular polygon of n sides. diameter is the diameter of a circle
'that would enclose the polygon touching its vertexes.
'the orientation is always zero degrees.
DEF angle,centralangle,length,apot as DOUBLE
DEF x1,y1,x2,y2 as DOUBLE
DEF count as INT
DEF pPoints as POINTER
pPoints = NEW(POINT,sides)

centralangle = 360.0/sides
angle=0
'the length of one side of the polygon
length = 2.0*(diameter/2.0)*sind(180.0/sides)
'the "apothem"  radius of an inscribed circle
apot = (diameter/2.0)*cosd(180.0/sides)
'calculate starting point
x1 = x - length / 2.0
y1 = y - apot
#<POINT>pPoints[0].x = x1
#<POINT>pPoints[0].y = y1
'
FOR count = 1 to sides-1
x2 = x1 + length * COSD(angle)
y2 = y1 + length * SIND(angle)
#<POINT>pPoints[count].x = x2
#<POINT>pPoints[count].y = y2
angle += centralangle
x1 = x2
y1 = y2
NEXT count

'handle rotation
FOR count = 0 to sides-1
x1 = ((#<POINT>pPoints[count].x-x) * cosd(rotation) - (#<POINT>pPoints[count].y-y) * sind(rotation))+x
y1 = ((#<POINT>pPoints[count].x-x) * sind(rotation) + (#<POINT>pPoints[count].y-y) * cosd(rotation))+y
#<POINT>pPoints[count].x = x1
#<POINT>pPoints[count].y = y1
NEXT count
SETLINESTYLE win, @LSSOLID, linewidth

FOR count = 0 to sides-2
LINE win,#<POINT>pPoints[count].x,#<POINT>pPoints[count].y,#<POINT>pPoints[count+1].x,#<POINT>pPoints[count+1].y,clr
NEXT count
LINE win,#<POINT>pPoints[sides-1].x,#<POINT>pPoints[sides-1].y,#<POINT>pPoints[0].x,#<POINT>pPoints[0].y,clr
DELETE pPoints

SETLINESTYLE win, @LSSOLID, 1

RETURN
ENDSUB

SUB Draw3DNPolygon(win as WINDOW,sides as int,x as INT,y as INT,diameter as DOUBLE,clr1 as UINT,clr2 as UINT,OPT linewidth=1 AS INT,opt rotation = 0.0 as DOUBLE)
'draws a regular polygon of n sides. diameter is the diameter of a circle
'that would enclose the polygon touching its vertexes.
DEF angle,centralangle,length,apot as DOUBLE
DEF x1,y1,x2,y2 as DOUBLE
DEF count as INT
DEF pPoints as POINTER
pPoints = NEW(POINT,sides)
centralangle = 360.0/sides
angle=0
'the length of one side of the polygon
length = 2.0*(diameter/2.0)*sind(180.0/sides)
'the "apothem"  radius of an inscribed circle
apot = (diameter/2.0)*cosd(180.0/sides)
'calculate starting point
x1 = x - length / 2.0
y1 = y - apot
#<POINT>pPoints[0].x = x1
#<POINT>pPoints[0].y = y1
'
FOR count = 1 to sides-1
x2 = x1 + length * COSD(angle)
y2 = y1 + length * SIND(angle)
#<POINT>pPoints[count].x = x2
#<POINT>pPoints[count].y = y2
angle += centralangle
x1 = x2
y1 = y2
NEXT count


'handle rotation
FOR count = 0 to sides-1
x1 = ((#<POINT>pPoints[count].x-x) * cosd(rotation) - (#<POINT>pPoints[count].y-y) * sind(rotation))+x
y1 = ((#<POINT>pPoints[count].x-x) * sind(rotation) + (#<POINT>pPoints[count].y-y) * cosd(rotation))+y
#<POINT>pPoints[count].x = x1
#<POINT>pPoints[count].y = y1
NEXT count
'draw first color
SETLINESTYLE win, @LSSOLID, linewidth
FOR count = 0 to sides-2
LINE win,#<POINT>pPoints[count].x,#<POINT>pPoints[count].y,#<POINT>pPoints[count+1].x,#<POINT>pPoints[count+1].y,clr1
NEXT count
LINE win,#<POINT>pPoints[sides-1].x,#<POINT>pPoints[sides-1].y,#<POINT>pPoints[0].x,#<POINT>pPoints[0].y,clr1

'adjust points around the origin to shrink by one
'angle towards center point for first vertex
angle = (180 - centralangle) / 2.0
angle += rotation
FOR count = 0 to sides-1
#pPoints[count].x+= linewidth*COSD(angle)
#pPoints[count].y+= linewidth*SIND(angle)
'all angles are equal
angle += centralangle
NEXT count

'draw second color
FOR count = 0 to sides-2
LINE win,#<POINT>pPoints[count].x,#<POINT>pPoints[count].y,#<POINT>pPoints[count+1].x,#<POINT>pPoints[count+1].y,clr2
NEXT count
LINE win,#<POINT>pPoints[sides-1].x,#<POINT>pPoints[sides-1].y,#<POINT>pPoints[0].x,#<POINT>pPoints[0].y,clr2

SETLINESTYLE win, @LSSOLID,1

DELETE pPoints
RETURN
ENDSUB



The two functions DrawNPolygon and Draw3DNPolygon will work with a normal window too, not just a 2D surface. 

Paul.
Ionic Wind Support Team

yujinwunz

thank you paul.

however i will be using the cos sin technique im already using, because later on i will be drawing irregular polygons

yujin