mike-obrien.net Curriculum Vitae Blog Labs
Saturday, September 02, 2006

The default pie drawing behavior in .NET does not create a pie slice that has 3d perspective when the bounds are rectangular. In order to give the pie slice 3d perspective you need to transform the angles using a little trig. I'm not a math expert so I'm not going to attempt to explan why/how the formulas work... ;-) Kudos to a ton of math sites and Julijan Sribar for his 3d pie chart for helping me figure this out. The following functions allow you to compute the angles for a 3d perspective:

Private Function To3dSweepAngle(ByVal Bounds As Rectangle, _
   ByVal Angle As Single, _
   ByVal SweepAngle As Single) As Single

   If SweepAngle Mod 180 <> 0 Then

      Dim Angle3d As Single = To3dAngle(Bounds, Angle)
      SweepAngle = To3dAngle(Bounds, Angle + SweepAngle) - Angle3d

   End If

   If SweepAngle < 0 Then

      SweepAngle += 360

   End If

   Return SweepAngle

End Function

Private Function To3dAngle(ByVal Bounds As System.Drawing.Rectangle, _
   ByVal Angle As Single) As Single

   Dim Radians As Single = ToRadian(Angle)
   Dim X As Double = Bounds.Width * Math.Cos(Radians)
   Dim Y As Double = Bounds.Height * Math.Sin(Radians)
   Dim Angle3D As Single = Math.Atan2(Y, X) * 180 / Math.PI

   If Angle3D < 0 Then

      Return Angle3D + 360

   Else

      Return Angle3D

   End If

End Function

Private Function ToRadian(ByVal Angle As Single) As Single

   Return (Math.PI * Angle) / 180

End Function

The following code snipet demonstrates how to draw a pie slice with 3d perspective:

Private Sub Form1_Paint(ByVal sender As Object, _
   ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

   Dim PieBounds As New Rectangle(20, 20, 400, 100)
   Dim PieAngle As Single = 150
   Dim PieSweepAngle As Single = 100

   '--> Determine the 3d angles
   PieSweepAngle = To3dSweepAngle(PieBounds, PieAngle, PieSweepAngle)
   PieAngle = To3dAngle(PieBounds, PieAngle)

   '--> Draw the pie
   e.Graphics.FillPie(Brushes.SteelBlue, PieBounds, PieAngle, PieSweepAngle)

End Sub


 

Saturday, September 02, 2006 8:05:07 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 

Recently I realized that the .NET LinearGradientBrush will not properly fill a pie slice with a sweep angle greater than 180 degrees. Instead you have to use a PathGradientBrush. It's pretty simple to do; just create a GraphicsPath which contains the path of your pie slice and pass it into the PathGradientBrush constructor. Then set your CenterPoint, CenterColor and SurroundColors, draw the slice with the PathGradientBrush and voilĂ !

Dim PieBounds As New Rectangle(20, 20, 200, 200)
Dim PieAngle As Single = 50
Dim PieSweepAngle As Single = 100
Dim PieCenterPoint As Point
Dim GradientPath As New System.Drawing.Drawing2D.GraphicsPath
Dim GradientBrush As System.Drawing.Drawing2D.PathGradientBrush

PieCenterPoint = New Point((PieBounds.Width / 2) + PieBounds.X, (PieBounds.Height / 2) + PieBounds.Y)

GradientPath.AddPie(PieBounds, PieAngle, PieSweepAngle)

GradientBrush = New System.Drawing.Drawing2D.PathGradientBrush(GradientPath)

GradientBrush.CenterPoint = PieCenterPoint

GradientBrush.CenterColor = Color.LightBlue
GradientBrush.SurroundColors = New Color() {Color.DarkBlue}

e.Graphics.FillPie(GradientBrush, PieBounds, PieAngle, PieSweepAngle)

Saturday, September 02, 2006 5:35:58 AM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 
Thursday, January 19, 2006

Thursday, January 19, 2006 11:39:09 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 
Thursday, January 12, 2006

Back in '98 Adobe offered a really cool web graphics product called Image Styler. It wasent the most powerful tool on the market but it had a very streamlined and simple interface and would allow you to create professional web graphics very quickly. I have used it for years now and love it, but unfortunately Adobe mothballed it in 2000 when they released LiveMotion (It's replacement). I never really liked LiveMotion, even though the two products were similar there were some quirky differences. In any event, LiveMotion (A competing product to Macromedia's lineup) was discontinued in '03 and Adobe and Macromedia merged and the rest is history. For those of you who are looking for the full version of ImageStyler you can download it here. It's pretty difficult to find these days which is too bad since IMO it was a great product.

Thursday, January 12, 2006 10:50:58 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [6]  |