Page 1 of 1

How to do Polygons in Purebasic?

Posted: Wed Apr 05, 2006 6:45 pm
by Michael Vogel
I miss some 2D functions in Purebasic, but they should be possible using the Windows API - if someone (but not me) does it right...

Code: Select all

Global win=OpenWindow(0,0,0,640,480,"3D",#PB_Window_ScreenCentered)
	
	Global Dim testp.l(10)
Global Dim testt.b(10)
testp(0)=100
testp(1)=100
testp(2)=300
testp(3)=200
testp(4)=400
testp(5)=400
testp(6)=100
testp(7)=100
testt(0)=#PT_MOVETO
testt(1)=#PT_LINETO
testt(2)=#PT_LINETO
testt(3)=#PT_LINETO
;ClearScreen(#White)
		PolyDraw_(win,@testp(),@testt(),3)

Repeat : Until WaitWindowEvent()=#WM_CHAR
[/code]

Posted: Wed Apr 05, 2006 6:49 pm
by Fred
You was close:

Code: Select all

Global win=OpenWindow(0,0,0,640,480,"3D",#PB_Window_ScreenCentered)
   
Global Dim testp.l(10)
Global Dim testt.b(10)
testp(0)=100
testp(1)=100
testp(2)=300
testp(3)=200
testp(4)=400
testp(5)=400
testp(6)=100
testp(7)=100
testt(0)=#PT_MOVETO
testt(1)=#PT_LINETO
testt(2)=#PT_LINETO
testt(3)=#PT_LINETO

  DC = StartDrawing(WindowOutput(0))
    PolyDraw_(DC,@testp(),@testt(),4)
  StopDrawing()

Repeat : Until WaitWindowEvent()=#WM_CHAR

Posted: Wed Apr 05, 2006 6:53 pm
by srod
The 2d drawing library can draw lines and so on direct to an image or a window.

Anyhow, if the API way is what you're after:

Code: Select all

OpenWindow(0,0,0,640,480,"3D",#PB_Window_ScreenCentered) 
    
   Global Dim testp.l(10) 
Global Dim testt.b(10) 
testp(0)=100 
testp(1)=100 
testp(2)=300 
testp(3)=200 
testp(4)=400 
testp(5)=400 
testp(6)=100 
testp(7)=100 
testt(0)=#PT_MOVETO 
testt(1)=#PT_LINETO 
testt(2)=#PT_LINETO 
testt(3)=#PT_LINETO 
;ClearScreen(#White) 
windc =GetDC_(WindowID(0))
  PolyDraw_(windc,@testp(),@testt(),3) 
Debug ReleaseDC_(WindowID(0), windc)
Repeat : Until WaitWindowEvent()=#WM_CHAR
The problem with your code is that you were passing the window handle to the PolyDraw_() function, when in fact you need to pass a handle to a device context. This is obtained either by the GetDC_() function or the Purebasic 'StartDrawing()' etc.

Edit: Fred, you're too quick sometimes! :D

Posted: Wed Apr 05, 2006 7:00 pm
by Michael Vogel
Extreeemly fast - because I've only a modem it will take much longer to do the tests now :?

The good thing for you all - because of my slow internet access it will take a while until I can ask the next thing...

Posted: Thu Apr 06, 2006 10:01 am
by Michael Vogel
Thanks again to all, because of your help, I'm able to do (nearly) what I wanted - drawing filled polygons...

* if there are API functions available for the Mac and for Linux also, it would be cool to implement this as a generic command in PureBasic...

* if there is a simple possibility to do transparent drawing, this would be also fine, but I fear this has to be done with OpenGL or so...

Code: Select all

Global win=OpenWindow(0,0,0,640,480,"3D",#PB_Window_ScreenCentered) 
    
Global Dim testp.l(10) 
Global Dim testt.l(10) 
testp(0)=100 : testp(1)=100 
testp(2)=300 : testp(3)=200 
testp(4)=400 : testp(5)=400 
testp(6)=100 : testp(7)=300
testt(0)=0 : testt(1)=200
testt(2)=500 : testt(3)=100
testt(4)=300 : testt(5)=300

  DC = StartDrawing(WindowOutput(0)) 
  brush=CreateSolidBrush_($ffa0a0)
  SelectObject_(dc,brush)
  ;SetPolyFillMode_(dc,#ALTERNATE)
  ;PolyDraw_(DC,@testp(),@testt(),4) 
  Polygon_(DC,@testp(),4) 
  Polygon_(DC,@testt(),3) 
  StopDrawing() 

Repeat : Until WaitWindowEvent()=#WM_CHAR 

Posted: Thu Apr 06, 2006 11:56 am
by GedB
At the very least, could we have a command to draw a filled triangle.

A procedure can easily be written to construct any other filled closed polygon.

Posted: Thu Apr 06, 2006 5:50 pm
by einander
Here is a simple Polygon without API.
But how to found a secure starting point for the FillArea command?

Code: Select all

Structure Polyg
    p.POINT
EndStructure
NewList Po.Polyg()

Procedure Polyg(Po.Polyg(),RGB)
    FirstElement(Po())
    x=Po()\p\x:y=Po()\p\y
    FirstX=x:FirstY=y
    While NextElement(Po()) 
        X1=Po()\p\x:Y1=Po()\p\y
        LineXY( x,y, X1,Y1)
        x=X1:y=Y1
    Wend
    LineXY(x,y,FirstX,FirstY) ; close shape
    
    FillArea(X1+1,Y1+1,0,RGB) ;Bad starting point *********  

EndProcedure 
         
        
        ;_____________________________
OpenWindow(0,0,0,0,0,"", #WS_OVERLAPPEDWINDOW | #WS_MAXIMIZE) 
StartDrawing(WindowOutput(0))  
        
Restore Polyg:
For i=1 To 10
    AddElement(Po())
    Read Po()\p\x:Read Po()\p\y
Next
Polyg(@Po(),#Green)
        
Repeat 
Until WaitWindowEvent()=#PB_Event_CloseWindow 
         
DataSection
Polyg:
Data.l 100,200,   250,200,   300,75,   350,200,   500,200,   400,300,   450,425,  300,350,  150,425,  200,300
enddatasection   
   

Posted: Fri Apr 07, 2006 10:19 am
by Michael Vogel
einander wrote:Here is a simple Polygon without API.
But how to found a secure starting point for the FillArea command?
It's math, just search for "Tesselation" in the internet - to divide polygons into triangles. If you have one triangle you just have to calculate its center point, this will be a save point, if the choosen tree points of the polygon are not on a single line...