November 28, 2020, 10:49:14 AM

Author Topic: [bmx] Circle/Arc drawing to pixmap by TomToad [ 2 months ago ]  (Read 441 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Circle/Arc drawing to pixmap
Author : TomToad
Posted : 2 months ago

Description : This code will draw circles and arcs onto pixmaps.  Just use Circle(Pixmap:TPixmap, xoff:int, y:off:int, radius:int, color:int, arcbegin:float = 0, arcend:float = 360, steps:int = 6)
Pixmap is the pixmap in which you will be drawing the circle
xoff, yoff will be the center of the circle
radius is the radius of the circle
color is in the format ARGB
arcbegin, arcend is the beginning angle and ending angle of the arc you wish to draw.
steps is the angle added each step.  A higher value will give a more polygon look, a lower value will give a smoother arc look.

The Line function is a modified version of <a href="codearcsca70.html?code=1465" >Besenham's LineDraw routine (integer math only)[/url] by ImaginaryHuman


Code :
Code: BlitzMax
  1. 'This functiom uses the Bresenham algorithm for drawing a line.  The code below is a
  2. ' slightly modified version of [codearc 1465] by ImaginaryHuman
  3. Function line(pixmap:TPixmap,x1:Int,y1:Int,x2:Int,y2:Int,color:Int)
  4.         'Draws a line of individual pixels from X1,Y1 to X2,Y2 at any angle
  5.         Local Steep:Int=Abs(Y2-Y1) > Abs(X2-X1)                 'Boolean
  6.         If Steep
  7.                 Local Temp:Int=X1; X1=Y1; Y1=Temp               'Swap X1,Y1
  8.                 Temp=X2; X2=Y2; Y2=Temp         'Swap X2,Y2
  9.         EndIf
  10.         Local DeltaX:Int=Abs(X2-X1)             'X Difference
  11.         Local DeltaY:Int=Abs(Y2-Y1)             'Y Difference
  12.         Local Error:Int=0               'Overflow counter
  13.         Local DeltaError:Int=DeltaY             'Counter adder
  14.         Local X:Int=X1          'Start at X1,Y1
  15.         Local Y:Int=Y1         
  16.         Local XStep:Int
  17.         Local YStep:Int
  18.         If X1<X2 Then XStep=1 Else XStep=-1     'Direction
  19.         If Y1<Y2 Then YStep=1 Else YStep=-1     'Direction
  20.         If Steep Then WritePixelSafe(pixmap,Y,X,color) Else WritePixelSafe(pixmap,X,Y,color)            'Draw
  21.         While X<>X2
  22.                 X:+XStep                'Move in X
  23.                 Error:+DeltaError               'Add to counter
  24.                 If (Error Shl 1)>DeltaX         'Would it overflow?
  25.                         Y:+YStep                'Move in Y
  26.                         Error=Error-DeltaX              'Overflow/wrap the counter
  27.                 EndIf
  28.                 If Steep Then WritePixelSafe(pixmap,Y,X,color) Else WritePixelSafe(pixmap,X,Y,color)            'Draw
  29.         Wend
  30. End Function
  31.  
  32. 'This function writes pixel color to the pixmap while also doing bounds checks
  33. Function WritePixelSafe(pixmap:TPixmap,x:Int,y:Int,color:Int)
  34.         If x < 0 Or x >= pixmap.Width Or y < 0 Or y >= pixmap.Height Then Return
  35.         WritePixel(pixmap,x,y,color)
  36. End Function
  37.  
  38. 'This function will draw a circle or an arc.
  39. 'pixmap is the pixmap that you will be drawing to
  40. 'xoff, yoff are the offsets for the center of the circle.  to center the circle on the pixmap, you
  41. '   would use something like xoff = pixmap.width/2 : yoff = pixmap.height/2
  42. 'radius is the radius of the circle
  43. 'color is the color you wish to plot in the format ARGB
  44. 'arcbegin, arcend are the beginning and ending of the arc. 0 is alligned to the positive x axis and
  45. '   angle increases clockwise.
  46. 'step is the number of degrees the renderer should advance each step.  The higher the number, the more polygonal
  47. '   the arc will look.  Using 90 will give you a square, using 120 will give you an equilateral triangle
  48. Function Circle(pixmap:TPixmap,xoff:Int,yoff:Int,radius:Int,color:Int,arcbegin:Float=0,arcend:Float=360,steps:Int = 6)
  49.         Local first:Int = True
  50.         Local ox:Int, oy:Int, x:Int, y:Int
  51.         While arcend < arcbegin
  52.                 arcend :+ 360
  53.         Wend
  54.         Local a:Float = arcbegin
  55.         While a < arcend
  56.                 x = xoff+radius*Cos(a)
  57.                 y = yoff+radius*Sin(a)
  58.                 If Not first
  59.                         line(pixmap,ox,oy,x,y,color)
  60.                 Else
  61.                         first = False
  62.                 End If
  63.                 ox = x
  64.                 oy = y
  65.                 a :+ steps
  66.         Wend
  67.         x = xoff + radius*Cos(arcend)
  68.         y = yoff + radius*Sin(arcend)
  69.         line(pixmap,ox,oy,x,y,color)
  70. End Function
  71.  
  72. 'example of usage
  73. Graphics 400,400
  74. Local pixmap:TPixmap = CreatePixmap(400,400,PF_RGBA8888)
  75. pixmap.ClearPixels($FFFFFFFF) 'fill the pixmap with white
  76.  
  77. Circle(pixmap,100,100,100,$FF000000) 'draw a black circle in the upper right corner
  78. Circle(pixmap,200,200,200,$FFFF0000,270,90) 'draw a red arc om the right
  79. circle(pixmap,300,300,50,$FF00FF00,0,360,72) 'draw a green pentagon
  80.  
  81. Cls
  82. DrawPixmap pixmap,0,0 'render the pixmap to the screen
  83. Flip
  84.  
  85. WaitKey() 'wait for a key to be pressed


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal