Why does this code not work?
Author |
Message |
Pirate Lor #1 / 5
|
 Why does this code not work?
Hi, I've some existing code for making a pie chart with 3 sections, which was plotted as follows. -rad, -rad1 -rad1, -(rad1+rad2) -(rad1+rad2), -(rad1+rad2+rad3) Not exactly the most efficent way. Inbetween each there is also a statement to change colour. So I stored the Rad1-3 values in an array and made a loop which produced the sequential rad totals. Now the chart will only show if I set the variables to Long, causing an incomplete pie (like a 4th slice is missing). Setting to Doubles (as it was originally) causes the program to crash. I'm a bit befuddled! If you can spot anything wrong with the below, then please let me know.... I know I've not defined all the variables, but it should still work. I'm using VB6 by the way. Pie is the name of the form (if you wanted to paste into a form) As far as I can tell, it should produce the same result as if I used 3 circle commands to produce the 3 arcs. My aim eventually is to produce more Arcs/Pie Wedges, so I don't want to have a seperate circle line command for each one Dim rads(1 To 3) As Double, Shade(1 To 3) As Long rad = 3.1415927 / 180 val1 = 50 val2 = 75 val3 = 25 rads(1) = val1 / (val1 + val2 + val3) * 360 * rad rads(2) = val2 / (val1 + val2 + val3) * 360 * rad rads(3) = val3 / (val1 + val2 + val3) * 360 * rad Shade(1) = RGB(255, 0, 0) Shade(2) = RGB(0, 255, 0) Shade(3) = RGB(0, 0, 255) Pie.FillStyle = 0 CurValue = 0 PrevValue = rad ScrWidth = Int(Pie.ScaleWidth / 2) ScrHeight = Int(Pie.ScaleHeight / 2) ScrRad = ScrWidth If ScrHeight < ScrWidth Then ScrRad = ScrHeight ScrRad = ScrRad - 60 For x = 1 To 3 Pie.FillColor = Shade(x) CurValue = PrevValue + rads(x) Pie.Circle (ScrWidth, ScrHeight), ScrRad, RGB(0, 0,0), -PrevValue, -CurValue PrevValue = CurValue Next x
|
Sat, 18 Dec 2004 01:27:18 GMT |
|
 |
pro.. #2 / 5
|
 Why does this code not work?
Debug.print and the message box are great tools to use when tracking down problems related to faulty logic and math. In your case since there is not much going on in the program so you can print out every single variable instance. pete Quote:
> Hi, > I've some existing code for making a pie chart with 3 sections, which was > plotted as follows. > -rad, -rad1 > -rad1, -(rad1+rad2) > -(rad1+rad2), -(rad1+rad2+rad3) > Not exactly the most efficent way. Inbetween each there is also a statement > to change colour. > So I stored the Rad1-3 values in an array and made a loop which produced the > sequential rad totals. Now the chart will only show if I set the variables > to Long, causing an incomplete pie (like a 4th slice is missing). Setting to > Doubles (as it was originally) causes the program to crash. > I'm a bit befuddled! > If you can spot anything wrong with the below, then please let me know.... > I know I've not defined all the variables, but it should still work. I'm > using VB6 by the way. > Pie is the name of the form (if you wanted to paste into a form) > As far as I can tell, it should produce the same result as if I used 3 > circle commands to produce the 3 arcs. My aim eventually is to produce more > Arcs/Pie Wedges, so I don't want to have a seperate circle line command for > each one > Dim rads(1 To 3) As Double, Shade(1 To 3) As Long > rad = 3.1415927 / 180 > val1 = 50 > val2 = 75 > val3 = 25 > rads(1) = val1 / (val1 + val2 + val3) * 360 * rad > rads(2) = val2 / (val1 + val2 + val3) * 360 * rad > rads(3) = val3 / (val1 + val2 + val3) * 360 * rad > Shade(1) = RGB(255, 0, 0) > Shade(2) = RGB(0, 255, 0) > Shade(3) = RGB(0, 0, 255) > Pie.FillStyle = 0 > CurValue = 0 > PrevValue = rad > ScrWidth = Int(Pie.ScaleWidth / 2) > ScrHeight = Int(Pie.ScaleHeight / 2) > ScrRad = ScrWidth > If ScrHeight < ScrWidth Then ScrRad = ScrHeight > ScrRad = ScrRad - 60 > For x = 1 To 3 > Pie.FillColor = Shade(x) > CurValue = PrevValue + rads(x) > Pie.Circle (ScrWidth, ScrHeight), ScrRad, RGB(0, > 0,0), -PrevValue, -CurValue > PrevValue = CurValue > Next x
|
Sat, 18 Dec 2004 02:29:40 GMT |
|
 |
Douglas Leininge #3 / 5
|
 Why does this code not work?
Pirate: First, the parameters for the Circle Method, as listed in the VB Online Reference, state that the center x & y, the radius, and the start and stop angles are single precision values. However, VB usually will cast them to single if you do not. Roundoff errors allow it to work with Long data types. The real problem is when you draw the third section, you exceed the limit of the circle command. The start and stop angles must be within the range -2 pi radians and 2 pi radians. When you run this as written, changing the array to Singles, the third CurValue ends up being 6.30063877480292 which is slightly larger that 2 * pi. If you add a check: If CurValue > 2 * 3.1415927 Then CurValue = 2 * 3.1415927 End If Then you can trap the error and correct it. However, you end up with a small gap at the end of the the last segment. Alternately, you can subtract 2 * pi from the value as: If CurValue > 2 * 3.1415927 Then CurValue = CurValue - (2 * 3.1415927) End If And you end up with the desired results. Always check the limits, both below -2 * pi and above 2 * pi to prevent crashes as you expand the capabilities to include more pie segments. Doug
Quote: > Hi, > I've some existing code for making a pie chart with 3 sections, which was > plotted as follows. > -rad, -rad1 > -rad1, -(rad1+rad2) > -(rad1+rad2), -(rad1+rad2+rad3) > Not exactly the most efficent way. Inbetween each there is also a statement > to change colour. > So I stored the Rad1-3 values in an array and made a loop which produced the > sequential rad totals. Now the chart will only show if I set the variables > to Long, causing an incomplete pie (like a 4th slice is missing). Setting to > Doubles (as it was originally) causes the program to crash. > I'm a bit befuddled! > If you can spot anything wrong with the below, then please let me know.... > I know I've not defined all the variables, but it should still work. I'm > using VB6 by the way. > Pie is the name of the form (if you wanted to paste into a form) > As far as I can tell, it should produce the same result as if I used 3 > circle commands to produce the 3 arcs. My aim eventually is to produce more > Arcs/Pie Wedges, so I don't want to have a seperate circle line command for > each one > Dim rads(1 To 3) As Double, Shade(1 To 3) As Long > rad = 3.1415927 / 180 > val1 = 50 > val2 = 75 > val3 = 25 > rads(1) = val1 / (val1 + val2 + val3) * 360 * rad > rads(2) = val2 / (val1 + val2 + val3) * 360 * rad > rads(3) = val3 / (val1 + val2 + val3) * 360 * rad > Shade(1) = RGB(255, 0, 0) > Shade(2) = RGB(0, 255, 0) > Shade(3) = RGB(0, 0, 255) > Pie.FillStyle = 0 > CurValue = 0 > PrevValue = rad > ScrWidth = Int(Pie.ScaleWidth / 2) > ScrHeight = Int(Pie.ScaleHeight / 2) > ScrRad = ScrWidth > If ScrHeight < ScrWidth Then ScrRad = ScrHeight > ScrRad = ScrRad - 60 > For x = 1 To 3 > Pie.FillColor = Shade(x) > CurValue = PrevValue + rads(x) > Pie.Circle (ScrWidth, ScrHeight), ScrRad, RGB(0, > 0,0), -PrevValue, -CurValue > PrevValue = CurValue > Next x
|
Sat, 18 Dec 2004 09:05:26 GMT |
|
 |
Yaniv Sapi #4 / 5
|
 Why does this code not work?
Quote:
> Hi, > I've some existing code for making a pie chart with 3 sections, which was > plotted as follows. > -rad, -rad1 > -rad1, -(rad1+rad2) > -(rad1+rad2), -(rad1+rad2+rad3) > Not exactly the most efficent way. Inbetween each there is also a statement > to change colour. > So I stored the Rad1-3 values in an array and made a loop which produced the > sequential rad totals. Now the chart will only show if I set the variables > to Long, causing an incomplete pie (like a 4th slice is missing). Setting to > Doubles (as it was originally) causes the program to crash. > I'm a bit befuddled! > If you can spot anything wrong with the below, then please let me know.... > I know I've not defined all the variables, but it should still work. I'm > using VB6 by the way. > Pie is the name of the form (if you wanted to paste into a form) > As far as I can tell, it should produce the same result as if I used 3 > circle commands to produce the 3 arcs. My aim eventually is to produce more > Arcs/Pie Wedges, so I don't want to have a seperate circle line command for > each one > Dim rads(1 To 3) As Double, Shade(1 To 3) As Long > rad = 3.1415927 / 180 > val1 = 50 > val2 = 75 > val3 = 25 > rads(1) = val1 / (val1 + val2 + val3) * 360 * rad > rads(2) = val2 / (val1 + val2 + val3) * 360 * rad > rads(3) = val3 / (val1 + val2 + val3) * 360 * rad > Shade(1) = RGB(255, 0, 0) > Shade(2) = RGB(0, 255, 0) > Shade(3) = RGB(0, 0, 255) > Pie.FillStyle = 0 > CurValue = 0 > PrevValue = rad > ScrWidth = Int(Pie.ScaleWidth / 2) > ScrHeight = Int(Pie.ScaleHeight / 2) > ScrRad = ScrWidth > If ScrHeight < ScrWidth Then ScrRad = ScrHeight > ScrRad = ScrRad - 60 > For x = 1 To 3 > Pie.FillColor = Shade(x) > CurValue = PrevValue + rads(x) > Pie.Circle (ScrWidth, ScrHeight), ScrRad, RGB(0, > 0,0), -PrevValue, -CurValue > PrevValue = CurValue > Next x
Can you isolate the line where the program crashes? Also, "Option Explicit" is your friend in such cases. Yaniv.
|
Sat, 18 Dec 2004 18:43:00 GMT |
|
 |
Jim Yab #5 / 5
|
 Why does this code not work?
FYI, PI = 4 * atn(1) is a lot easier and probably more accurate than trying to remember the numeral, PI = 3.14159265358979323846+, but who cares when you can use 4 times the ArcTan of one radian. In this case you do not need the accuracy, but it is something to remember in future use of PI and easier to type. Jim
Quote:
> Hi, > I've some existing code for making a pie chart with 3 sections, which was > plotted as follows. > -rad, -rad1 > -rad1, -(rad1+rad2) > -(rad1+rad2), -(rad1+rad2+rad3) > Not exactly the most efficent way. Inbetween each there is also a statement > to change colour. > So I stored the Rad1-3 values in an array and made a loop which produced the > sequential rad totals. Now the chart will only show if I set the variables > to Long, causing an incomplete pie (like a 4th slice is missing). Setting to > Doubles (as it was originally) causes the program to crash. > I'm a bit befuddled! > If you can spot anything wrong with the below, then please let me know.... > I know I've not defined all the variables, but it should still work. I'm > using VB6 by the way. > Pie is the name of the form (if you wanted to paste into a form) > As far as I can tell, it should produce the same result as if I used 3 > circle commands to produce the 3 arcs. My aim eventually is to produce more > Arcs/Pie Wedges, so I don't want to have a seperate circle line command for > each one > Dim rads(1 To 3) As Double, Shade(1 To 3) As Long > rad = 3.1415927 / 180 > val1 = 50 > val2 = 75 > val3 = 25 > rads(1) = val1 / (val1 + val2 + val3) * 360 * rad > rads(2) = val2 / (val1 + val2 + val3) * 360 * rad > rads(3) = val3 / (val1 + val2 + val3) * 360 * rad > Shade(1) = RGB(255, 0, 0) > Shade(2) = RGB(0, 255, 0) > Shade(3) = RGB(0, 0, 255) > Pie.FillStyle = 0 > CurValue = 0 > PrevValue = rad > ScrWidth = Int(Pie.ScaleWidth / 2) > ScrHeight = Int(Pie.ScaleHeight / 2) > ScrRad = ScrWidth > If ScrHeight < ScrWidth Then ScrRad = ScrHeight > ScrRad = ScrRad - 60 > For x = 1 To 3 > Pie.FillColor = Shade(x) > CurValue = PrevValue + rads(x) > Pie.Circle (ScrWidth, ScrHeight), ScrRad, RGB(0, > 0,0), -PrevValue, -CurValue > PrevValue = CurValue > Next x
Can you isolate the line where the program crashes? Also, "Option Explicit" is your friend in such cases. Yaniv.
|
Sun, 19 Dec 2004 10:09:50 GMT |
|
|
|