所以我试图用极坐标在披萨上画胡椒,但是我在移动原点方面遇到了问题。这是我的代码:
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
这就是我画胡椒的方法:
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
这是我的结果:
在开始绘画之前,我是如何在极坐标上移动原点的?我很难绕过这个。
答案 0 :(得分:1)
您必须计算比萨饼的中心并将其添加到peperoni x和y。
PTC.X += ( pie.X + pie.Width ) \ 2 ' "\" for integer division instead of "/"
PTC.Y += ( pie.Y + pie.Height ) \ 2
此外,通过极坐标,馅饼的中心看起来比两侧有更多的意大利辣味。相反,你可以随机获得X和Y,并检查它们是否在披萨派圈内。
<小时/> 要在.NET中获得0到2之间的随机整数:
Dim random as New Random()
Dim value = random.Next(3);