0

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.

Dunnage
  • 99
  • 7

1 Answers1

1

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);
Slai
  • 22,144
  • 5
  • 45
  • 53
  • 2
    To avoid bunching up the toppings toward the centre, the radius can be chosen with `Dim r = Math.Sqrt(CDbl(rand.Next()) / Integer.MaxValue) * rMax` where `rand = New Random()` and `rMax` is the maximum allowed radius (probably rPizza - rPepperoni). Ref: [Random points inside a circle](http://stackoverflow.com/a/29061071/1115360). – Andrew Morton Jul 03 '16 at 15:58
  • Thanks a ton. I couldn't wrap my head around the last part... I guess setting up the polar coordinates fried my brain for the day. – Dunnage Jul 04 '16 at 13:21