polygon include

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
#NULL
Beiträge: 2238
Registriert: 20.04.2006 09:50

polygon include

Beitrag von #NULL »

ich pack hier einfach mal ne polygon.pbi von mir rein, auch wenn sie veilleicht etwas kompliziert ist.
tut ja keinem weh <)

Code: Alles auswählen

; polygon.pbi

;XIncludeFile "E:\pbi\getAngle.pbi"
;{ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ E:\pbi\getAngle.pbi
        ; getAngle.pbi
        
        
        Procedure.f getAngle(mx,my, px,py, mode.l=1)
        ; returns the angle of a line going from point
        ; [mx,my] to point [px,py] in radian
        ; mode=1 (default):
        ; pointing to the right means 0 degrees
        ; pointing to the top means 90 degrees, ect
        ; [math y-axis]
        ; mode=-1:
        ; pointing to the right means 0 degrees
        ; pointing to the bottom means 90 degrees, ect
        ; [screen y-axis.]
        
          Static xd.l, yd.l, alpha.f
          xd=px-mx
          yd=py-my
          
          If xd>0
            If     yd<0 :: alpha=         ATan(-yd / xd )
            ElseIf yd>=0 :: alpha= 2*#PI - ATan( yd / xd )
            EndIf
          ElseIf xd<0
            If     yd<0 :: alpha=   #PI - ATan(-yd /-xd )
            ElseIf yd>=0 :: alpha=   #PI + ATan( yd /-xd )
            EndIf
          ElseIf xd=0
            If     yd>0 : alpha=   #PI*1.5
            ElseIf yd<0 : alpha=   #PI*0.5
            EndIf
          EndIf
          If mode=-1
            alpha = 2*#PI-alpha
          EndIf
        ;   If Not Random(200)
        ;     Debug Str(xd) + " | " + Str(yd) + " | " + StrF(alpha)
        ;   EndIf
          ProcedureReturn alpha
        EndProcedure
        
        
        ; [...]
;}
;  ________________________________/ [end] E:\pbi\getAngle.pbi



Structure _S_poly
  p0.POINT
  p.POINT
  go.POINT
  center.POINT
  radius.l
  angle.f
  color.l
  relative.l
  temp.f
EndStructure
Global _poly._S_poly





; 'initPoly(..)' sets the initial coordiantes for a new polygon.
; you can also set a color (default is white)
; set 'relative_' to #FALSE if following calls of 'setPoly(..)'
; use absolute coordinates. if set to #TRUE (default) 'setPoly(..)'
; moves relative to its current position
Macro initPoly(x_, y_, color_=$ffffff, relative_=#True)
  _poly\p0\x = x_
  _poly\p0\y = y_
  _poly\p\x  = x_
  _poly\p\y  = y_
  _poly\color = color_
  _poly\relative = relative_
  _poly\angle = 0
EndMacro


; 'setPoly(..)' sets a new point / adds a segment.
; paramters 'x_'/'y_' are used as absolute (i.e.:screen-coordinates)
; or relaitve to the current position, according to what was defined
; in 'initPoly(..)'
; you can choose a optional color for this segment.
Macro setPoly(x_, y_, color_=_poly\color)
  _poly\go\x = (x_)+(0 Or _poly\relative)*_poly\p\x
  _poly\go\y = (y_)+(0 Or _poly\relative)*_poly\p\y
  LineXY( _poly\p\x, _poly\p\y, _poly\go\x, _poly\go\y, color_)
  _poly\p\x =  _poly\go\x
  _poly\p\y =  _poly\go\y
EndMacro


; 'setPoly2(..)' sets a new point / adds a segment.
; paramter 'a_' is the angle of the segment relative to
; the current angle. 'r_' is the length of the segment
; you can choose an optional color for this segment.
; 'a_' is always relative.
; 'r_' is always absolute
Macro setPolyA(a_, r_, color_=_poly\color)
  _poly\radius = (r_)
  _poly\angle  + (a_)
  Line( _poly\p\x, _poly\p\y, _poly\radius*Cos(_poly\angle), _poly\radius*Sin(_poly\angle), color_)
  _poly\p\x + _poly\radius*Cos(_poly\angle)
  _poly\p\y + _poly\radius*Sin(_poly\angle)
EndMacro


; 'closePoly()' is not required, but can be used to easily close
; a polygon, that is to say to draw a line from the current postion
; back to the initial position.
; you can choose an optional color for this segment.
Macro closePoly(color_=_poly\color)
  LineXY(_poly\p\x, _poly\p\y, _poly\p0\x, _poly\p0\y, color_)
EndMacro







Macro initRadialPolyA(xCenter_, yCenter_, a_, r_, color_=$ffffff, relative_=#True)
  _poly\center\x = xCenter_
  _poly\center\y = yCenter_
  _poly\p0\x = _poly\center\x + (r_) * Cos(a_)
  _poly\p0\y = _poly\center\y + (r_) * Sin(a_)
  _poly\p\x  = _poly\p0\x
  _poly\p\y  = _poly\p0\y
  _poly\color = color_
  _poly\relative = relative_
  _poly\angle = a_
EndMacro


Macro setRadialPolyA(a_, r_, color_=_poly\color)
  _poly\angle = (a_) + (0 Or _poly\relative)*_poly\angle
  _poly\go\x = _poly\center\x + (r_) * Cos(_poly\angle)
  _poly\go\y = _poly\center\y + (r_) * Sin(_poly\angle)
  LineXY( _poly\p\x, _poly\p\y, _poly\go\x, _poly\go\y, color_)
  _poly\p\x = _poly\go\x
  _poly\p\y = _poly\go\y
EndMacro


Macro initRadialPoly(xCenter_, yCenter_, x_, y_, a_, color_=$ffffff, relative_=#True)
  _poly\center\x = xCenter_
  _poly\center\y = yCenter_
  _poly\angle = a_
  _poly\temp = getAngle(_poly\center\x, _poly\center\y, _poly\center\x + (x_), _poly\center\y + (y_),-1)
  _poly\radius = Sqr( (x_)*(x_)+(y_)*(y_) )
  _poly\p0\x = _poly\center\x + _poly\radius * Cos(_poly\angle + _poly\temp)
  _poly\p0\y = _poly\center\y + _poly\radius * Sin(_poly\angle + _poly\temp)
  _poly\p\x  = _poly\p0\x
  _poly\p\y  = _poly\p0\y
  _poly\color = color_
  _poly\relative = relative_
EndMacro


Macro setRadialPoly(x_, y_, color_=_poly\color)
  _poly\temp = getAngle(_poly\center\x, _poly\center\y, _poly\center\x + (x_), _poly\center\y + (y_),-1)
  _poly\radius = Sqr( (x_)*(x_)+(y_)*(y_) )
  _poly\go\x = _poly\center\x + _poly\radius * Cos(_poly\angle + _poly\temp)
  _poly\go\y = _poly\center\y + _poly\radius * Sin(_poly\angle + _poly\temp)
  LineXY( _poly\p\x, _poly\p\y, _poly\go\x, _poly\go\y, color_)
  _poly\p\x = _poly\go\x
  _poly\p\y = _poly\go\y
EndMacro



Macro rad(_deg_)
  ((_deg_)*0.017453292519943295) ; _deg_ * #PI / 180
EndMacro


; [merge_end]









; ;EXAMPLE
; 
; hWin=OpenWindow(0, 50,50,800,700, "EXAMPLE - polygon.pbi")
; CreateGadgetList(hWin)
; ImageGadget(0, 0,0,  0,0, CreateImage(0,800,700), #PB_Image_Border)
; AddKeyboardShortcut(0,#PB_Shortcut_Escape,0)
; font=LoadFont(0,"Arial",8)
; Repeat
;   StartDrawing( ImageOutput(0) )
;     Box(0,0,WindowWidth(0),WindowHeight(0),$000000)
;     
;     
;     ;a triangle
;     
;     initPoly(50,300,$ff0000)
;     setPoly(100,0)
;     setPoly(-100,100)
;     closePoly()
;     
;     
;     ;a polygon
;     
;     initPoly(140,20,$ffff00,#False)
;     setPoly(190,70)
;     setPoly(240,70)
;     setPoly(240,20,$00ff00)
;     setPoly(270,20)
;     setPoly(270,220)
;     setPoly(220,220,$0000ff)
;     setPoly( 20,20,$0000ff)
;     setPoly(140,20)
;     
;     
;     ;a spiral
;     
;     amount = DesktopMouseY()/4
;     angle.f = DesktopMouseX()/10
;     initPoly(400,200)
;     For i=1 To amount
;       setPolyA( rad(angle), i, RGB(255,255*i/amount,0) )
;     Next
;     
;     
;     ;a bzzz
;     
;     For n=0 To 4
;       initPoly(100,500)
;       For i=0 To 150
;         setPoly(4, Random(8)-4, $ff0000+$0000ff*i/150)
;       Next
;     Next
;     
;     
;     ;a whirling star
;     
;     size=50
;     initPoly(300,350,$00ff00)
;     For n=1 To DesktopMouseX()/30
;       setPolyA(rad(175),size)
;       setPolyA(rad(-160),size+(DesktopMouseY()-400)/40)
;     Next
;     
;     
;     ;an angled box
;     
;     width=180
;     height=40
;     angle=DesktopMouseX()/10
;     initPoly(400,400)
;     setPolyA(rad(angle),width)
;     setPolyA(rad(90),height)
;     setPolyA(rad(90),width)
;     closePoly()
;     
;     
;     ;another angled box (centered)
;     
;     a=20
;     b=80
;     angle=DesktopMouseX()/10
;     Circle(450,510,3,$00ff00)
;     initRadialPolyA(450,510,rad(angle),50)
;     setRadialPolyA(rad(180*a/b),50)
;     setRadialPolyA(rad(180-180*a/b),50)
;     setRadialPolyA(rad(180*a/b),50)
;     closePoly()
;     
;     
;     ;an angled ellipse
;     
;     r1    = 100
;     r2    = 50
;     angle = DesktopMouseY()
;     min   = DesktopMouseX()
;     max   = min+356-45
;     Circle(200,600,3,$0000ff)
;     initRadialPoly(200,600,r1*Cos(rad(min)),r2*Sin(rad(min)),rad(angle),$0000ff)
;     For i=min To max
;       setRadialPoly(r1*Cos(rad(i)),r2*Sin(rad(i)))
;     Next
;     ;closePoly()
;     
;     
;   StopDrawing()
;   SetGadgetState(0,ImageID(0))
;   event=WaitWindowEvent(40)
;   Select event
;     Case #PB_Event_CloseWindow
;       quit=1
;     Case #PB_Event_Menu
;       quit=1
;   EndSelect
; Until quit
my pb stuff..
Bild..jedenfalls war das mal so.