Search code examples
vb.netvisual-studiopolar-coordinates

Move point of origin while drawing using polar coordinates in visual basic


So I'm trying to draw pepperonis on a pizza using polar coordinates but I'm having an issue moving the point of origin. Here's my code:

Public Class polarToCartesian

Property X As Double
Property Y As Double
Sub New(Radius As Double, AngleDegree As Double)
    Dim AngleRadian As Double = AngleDegree * 2 * Math.PI / 360
    X = Radius * Math.Cos(AngleRadian)
    Y = Radius * Math.Sin(AngleRadian)
End Sub
End Class

Public Class CartesianToPolar
    Property Radius As Double
    Property AngleRadian As Double
    ReadOnly Property AngleDegree As Double
    Get
        Return AngleRadian * 360 / (2 * Math.PI)
    End Get
    End Property

    Sub New(X As Double, Y As Double)
        Radius = (X ^ 2 + Y ^ 2) ^ 0.5
        AngleRadian = Math.Atan2(Y, X)
    End Sub
End Class

And here's how I'm drawing the pepperonis:

If pTopping = True Then
    Dim counter = 0
    Dim imgPic(3) As Image
    imgPic(0) = pepperoniOne
    imgPic(1) = pepperoniTwo
    imgPic(2) = pepperoniThree
    Do Until counter > 38
        Dim value As Integer = CInt(Int((3 * Rnd()) + 0))
        Dim i = imgPic(value)
        Dim PTC As New polarAndCartesian(CInt(Int((60 * Rnd()) + 0)), CInt(Int((360 * Rnd()) + 0)))
        e.Graphics.DrawImage(i, CInt(PTC.X), CInt(PTC.Y))
        counter += 1
    Loop
End If

And here's my results:

enter image description here

Any Idea how I move the point of origin on the polar coordinates before I begin drawing? I'm having a hard time wrapping my head around this one.


Solution

  • You have to calculate the center of the pizza pie and add it to the peperoni x and y.

    PTC.X += ( pie.X + pie.Width  ) \ 2 ' "\" for integer division instead of "/"
    PTC.Y += ( pie.Y + pie.Height ) \ 2 
    

    Also, with the polar coordinates the center of the pie will seem like it has more peperoni than the sides. Instead, you can just get random X and Y and check if they are within the pizza pie circle.


    To get random integer between 0 and 2 in .NET :

    Dim random as New Random()
    Dim value = random.Next(3);