DrawRegularPolygon() 2D function

Share your advanced PureBasic knowledge/code with the community.
User avatar
Psychophanta
Addict
Addict
Posts: 4997
Joined: Wed Jun 11, 2003 9:33 pm
Location: Lípetsk, Russian Federation
Contact:

DrawRegularPolygon() 2D function

Post by Psychophanta »

Another newbye style coding tip:

DrawRegularPolygon() 2D function:

Code: Select all

Procedure DrawRegularPolygon(x.f,y.f,r.f,n.l,fase.f,color.l)
  ; 'x,y' <- center of polygon
  ; 'r' <- radius
  ; 'n' <- number of sides
  ; 'fase' <- first vertex angle position
  Protected a.f=2*#PI/n,t.l,d0.f,d.f=fase
  For t.l=1 To n
    d0=d
    d=t*a+fase
    LineXY(x+r*Cos(d0),y+r*Sin(d0),x+r*Cos(d),y+r*Sin(d),color)
  Next
EndProcedure
Testing it:

Code: Select all

InitMouse():InitSprite():InitKeyboard()
OpenScreen(GetSystemMetrics_(#SM_CXSCREEN),GetSystemMetrics_(#SM_CYSCREEN),32,"Draw Regular Polygon")

Procedure DrawRegularPolygon(x.f,y.f,r.f,n.l,fase.f,color.l)
  ; 'x,y' <- center of polygon
  ; 'r' <- radius
  ; 'n' <- number of sides
  ; 'fase' <- first vertex angle position
  Protected a.f=2*#PI/n,t.l,d0.f,d.f=fase
  For t.l=1 To n
    d0=d
    d=t*a+fase
    LineXY(x+r*Cos(d0),y+r*Sin(d0),x+r*Cos(d),y+r*Sin(d),color)
  Next
EndProcedure

r1.f=170:p.l=3
MouseLocate(333,333)
Repeat
  ClearScreen(0)
  ExamineKeyboard()
  ExamineMouse():x.l=MouseX():y.l=MouseY()
  StartDrawing(ScreenOutput())
  DrawRegularPolygon(x,y,r1.f,p,fa.f,$aaaaaa)
  If MouseButton(#PB_MouseButton_Middle)
    fa+MouseWheel()*0.1
  Else
    r1+MouseWheel()*10
  EndIf
  If MouseButton(#PB_MouseButton_Right):While MouseButton(#PB_MouseButton_Right):Delay(16):ExamineMouse():Wend
    p+1:If p>80:p=80:EndIf
  ElseIf MouseButton(#PB_MouseButton_Left):While MouseButton(#PB_MouseButton_Left):Delay(16):ExamineMouse():Wend
    p-1:If p<2:p=2:EndIf
  EndIf
  StopDrawing()
  FlipBuffers():Delay(16)
Until KeyboardPushed(#PB_Key_Escape)
http://www.zeitgeistmovie.com

While world=business:world+mafia:Wend
Will never leave this forum until the absolute bugfree PB :mrgreen: