Create 2D Polygon

Just starting out? Need help? Post your questions and find answers here.
User avatar
Mischa
Enthusiast
Enthusiast
Posts: 115
Joined: Fri Aug 15, 2003 7:43 pm

Create 2D Polygon

Post by Mischa »

Hi!

I wonder if i've overseen something, cause i need a polygon function in 2Ddrawing, but it isn't there?
The vector lib is not perfect, cause i need the polys in combination with a pixel analyse of bitmaps.
Another problem is that i don't know before how many points the poly will have, so i need linked lists.
I wrote this simple but surely unperfect code. Can it be done better/faster than that? :?

Regards,
Mischa

Code: Select all

Procedure Polygon(_hdc.l, List _polys.Point(),_pensize,_pencolor,_brushcolor)
  _polycount=ListSize(_polys())
  _memory=AllocateMemory(_polycount*8)
  _count=0
  ForEach _polys()
    PokeL(_memory+_count,_polys()\x)
    PokeL(_memory+_count+4,_polys()\y)
    _count+8
  Next
  _pen=CreatePen_(#PS_SOLID, _pensize, _pencolor)
  _brush=CreateSolidBrush_(_brushcolor)
  _oldpen=SelectObject_(_hdc,_pen)
  _oldbrush=SelectObject_(_hdc,_brush)
  Polygon_(_hdc,_memory,_polycount)
  SelectObject_(_hdc,_oldpen)
  SelectObject_(_hdc,_oldbrush)
  DeleteObject_(_pen)
  DeleteObject_(_brush)
  FreeMemory(_memory)
EndProcedure

Procedure AddPolyPoint(List _polys.POINT(), _polyx, _polyy)
  AddElement(_polys())
  _polys()\x = _polyx
  _polys()\y = _polyy
EndProcedure


NewList mypolys.Point()
AddPolyPoint(mypolys(),80 , 150)
AddPolyPoint(mypolys(),150, 60)
AddPolyPoint(mypolys(),200, 80)
AddPolyPoint(mypolys(),270, 200)
AddPolyPoint(mypolys(),200, 270)
AddPolyPoint(mypolys(),170, 180)


If OpenWindow(0, 0, 0, 320, 320, "Polygon Demo", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  CanvasGadget(0, 10, 10, 300, 300)
  
  hdc=StartDrawing(CanvasOutput(0))
    Box(30,30,200,200,RGB(0,255,0))
    Polygon(hdc, mypolys(), 3, RGB(0,0,0), RGB(200,128,200))
  StopDrawing()  
  
  Repeat
    Event = WaitWindowEvent()
      
  Until Event = #PB_Event_CloseWindow
EndIf