PDA

View Full Version : primes spiral with labels (canvas example)



zak
30-07-2010, 09:53
Hi
i refer to a previous post here http://community.thinbasic.com/index.php?topic=3437.0
about plotting prime numbers spiral, i refer to the Eros modification on using canvas_wait and printing text on the canvas on a specified place, so i have used this feature to print the numbers on the big circles representing the spiral, this is usefull for educational purposes. it is usefull to put a label on a triangle or a square ...etc.
you can change the font size and color and background color.


Uses "UI"

Dim hWin As DWord = Canvas_Window("primes spiral - press any key to Exit", 1, 1, 800, 600 )
Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer
Dim t1, t2 As Quad
Canvas_Clear(%BLACK)
Dim i,j,colrR,colrG,colrB As Long
Dim x, y, x2, y2, dots As Long
x=400:y=300
Dim turn,num,points_per_edge,edgecycle As Long
turn=0:num=1
points_per_edge = 1: edgecycle = 0
'ploting point number 1 :
Canvas_Ellipse(x-4, y-4, x+4, y+4, Rgb(255, 255, 0),Rgb(255, 255, 0))
Canvas_Redraw

For i = 1 To 23

Plotting()

Next


Canvas_WaitKey


Canvas_Window End


Sub Plotting()

turn = turn + 1
If turn = 5 Then turn = 1
edgecycle = edgecycle + 1

If edgecycle = 3 Then
edgecycle = 1
points_per_edge = points_per_edge + 1
End If

For j = 1 To points_per_edge

num = num + 1

If IsPrime(num)=%TRUE Then
colrR = 0 : colrG = 255 : colrB = 0
Else
colrR = 255 : colrG = 0 : colrB = 0
End If

Select Case turn
Case 1
x = x + 48
Case 2
y = y - 48
Case 3
x = x - 48
Case 4
y = y + 48
End Select

Canvas_Ellipse(x-20, y-20, x+20, y+20, Rgb(colrR, colrG, colrB),Rgb(colrR, colrG, colrB))
x2 = x-10: y2 = y-10
dots = dots + 1
Canvas_SetPos(x2, y2)
Canvas_Color(Rgb(0, 0, 0),Rgb(205, 242, 254))
Canvas_Font("Arial CE" , 12, %CANVAS_FONTSTYLE_BOLD)

Canvas_Print Str$(num)

Canvas_Redraw
Next j


End Sub

Petr Schreiber
30-07-2010, 11:40
Hi Zak,

that is good!
I did little tweak on number alignment and look:


Uses "UI"

Dim i,j,colrR,colrG,colrB As Long
Dim x, y, x2, y2, dots As Long
Dim turn,num,points_per_edge,edgecycle As Long
Dim hWin As DWord = Canvas_Window("primes spiral - press any key to Exit", 1, 1, 800, 600 )
Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer

Dim t1, t2 As Quad

Canvas_Clear(%BLACK)

x=400:y=300
turn=0:num=1
points_per_edge = 1: edgecycle = 0

'ploting point number 1 :
Canvas_Ellipse(x-4, y-4, x+4, y+4, Rgb(255, 255, 0),Rgb(255, 255, 0))
Canvas_Redraw

For i = 1 To 23

Plotting()

Next

Canvas_WaitKey
Canvas_Window End


Sub Plotting()

turn = turn + 1
If turn = 5 Then turn = 1
edgecycle = edgecycle + 1

If edgecycle = 3 Then
edgecycle = 1
points_per_edge = points_per_edge + 1
End If

For j = 1 To points_per_edge

num = num + 1

If IsPrime(num)=%TRUE Then
colrR = 0 : colrG = 255 : colrB = 0
Else
colrR = 255 : colrG = 0 : colrB = 0
End If

Select Case turn
Case 1
x = x + 48
Case 2
y = y - 48
Case 3
x = x - 48
Case 4
y = y + 48
End Select

Canvas_Ellipse(x-20, y-20, x+20, y+20, Rgb(colrR, colrG, colrB),Rgb(colrR, colrG, colrB))
x2 = x-10: y2 = y-10
dots = dots + 1

Canvas_Font("Arial CE" , 12, %CANVAS_FONTSTYLE_BOLD)

' -- Empiric alignment fix according to number length
Select Case Len(Format$(num))
Case 1
x2 += 2

Case 2
x2 -= 4

Case 3
x2 -= 8

End Select

Canvas_SetPos(x2, y2)
Canvas_Color(Rgb(0, 0, 0),-2)
Canvas_Print Str$(num)

Canvas_SetPos(x2+1, y2+1)
Canvas_Color(Rgb(255, 255, 255),-2)
Canvas_Print Str$(num)

Canvas_Redraw
Next j

End Sub



Petr;

zak
30-07-2010, 21:07
thanks Petr very much, i love very much the way you have displayed the font , and the correctly displayed numbers on the circles, much more visually appealed.
thanks