Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » PieSlice - Useful source for people (and maybe inclusion in SWT?)
PieSlice - Useful source for people (and maybe inclusion in SWT?) [message #441734] Mon, 23 August 2004 00:36 Go to next message
Andrew Brampton is currently offline Andrew BramptonFriend
Messages: 8
Registered: July 2009
Junior Member
Hi,
I've been trying to draw pie slices for a pie chat. The slice consists of
the arc, and two radius length lines...

I had it drawing fine with fillArc and drawArc, however it had two problems.

A) The filled area of the slice using fillArc didn't always reach the line
drawn by drawArc, there were sometimes a few white background pixels showing
at the very edges.

B) There was no easy way to draw the radius lines.

I solved problem B by borrowing some code from GC.java and using that to
work out the x1,x2,y1,y2 positions of the lines, and then using
drawLine(...)

Problem A I couldn't solve, I figured it could be a mistake in the SWT
Source in how it works out the positions, but looking at both fillArc and
drawArc showed near identical code, so I don't think its SWT fault, just a
(Windows) platform fault...

ANYWAY

I ended up writing my own function very similar to fill/drawArc, but with a
couple of minor changes... This uses the system's pie function to draw the
entire slice. Here is my source, it differs from fillArc in the fact that it
removes two lines (the ones disabling the system's pen).

public void drawPieSlice(GC gc, int x, int y, int width, int height, int
startAngle, int arcAngle) {
if (width < 0) {
x = x + width;
width = -width;
}
if (height < 0) {
y = y + height;
height = -height;
}
if (width == 0 || height == 0 || arcAngle == 0)
return;

int x1, y1, x2, y2, tmp;
boolean isNegative;
if (arcAngle >= 360 || arcAngle <= -360) {
x1 = x2 = x + width;
y1 = y2 = y + height / 2;
} else {
isNegative = arcAngle < 0;

arcAngle = arcAngle + startAngle;
if (isNegative) {
// swap angles
tmp = startAngle;
startAngle = arcAngle;
arcAngle = tmp;
}
x1 = Compatibility.cos(startAngle, width / 2) + x + width / 2;
y1 = -1 * Compatibility.sin(startAngle, height / 2) + y + height
/ 2;

x2 = Compatibility.cos(arcAngle, width / 2) + x + width / 2;
y2 = -1 * Compatibility.sin(arcAngle, height / 2) + y + height /
2;
}

OS.Pie(gc.handle, x, y, x + width + 1, y + height + 1, x1, y1, x2,
y2);
}

Also I've just noted that I removed the winCE support.

I'm post this code here for 3 reasons.
1) People may find it handy if they are doing a similar task
2) Someone may want to add it to the GC class
3) I might have been stupid at solving problem A, and a existing solution
exists... If so can someone highlight the error of my ways...

Thanks
Andrew

P.S I noticed a few lines of redundant code in drawArc:

int nullBrush = OS.GetStockObject(OS.NULL_BRUSH);
int oldBrush = OS.SelectObject(handle, nullBrush);
OS.Arc(handle, x, y, x + width + 1, y + height + 1, x1, y1, x2, y2);
OS.SelectObject(handle, oldBrush);

Changing the brush has NO effect on Arc method, and as such is not
needed.... If however the user was using OS.Pie instead then these extra
lines would be needed.... Since this is not a bug, I don't know whether I
should add it to the bugs system just to point out that its not needed.
Re: PieSlice - Useful source for people (and maybe inclusion in SWT?) [message #441751 is a reply to message #441734] Mon, 23 August 2004 17:58 Go to previous messageGo to next message
Silenio Quarti is currently offline Silenio QuartiFriend
Messages: 31
Registered: July 2009
Member
Hi Andrew,

Please open a bug report with this information. I will investigate it.
Maybe if we called Pie() in both drawArc and fillArc, problem A) would not
happen. Could you try it? I will check if the null brush is not needed
anyways.

Thanks!
Silenio



Andrew Brampton wrote:

> Hi,
> I've been trying to draw pie slices for a pie chat. The slice consists of
> the arc, and two radius length lines...

> I had it drawing fine with fillArc and drawArc, however it had two problems.

> A) The filled area of the slice using fillArc didn't always reach the line
> drawn by drawArc, there were sometimes a few white background pixels showing
> at the very edges.

> B) There was no easy way to draw the radius lines.

> I solved problem B by borrowing some code from GC.java and using that to
> work out the x1,x2,y1,y2 positions of the lines, and then using
> drawLine(...)

> Problem A I couldn't solve, I figured it could be a mistake in the SWT
> Source in how it works out the positions, but looking at both fillArc and
> drawArc showed near identical code, so I don't think its SWT fault, just a
> (Windows) platform fault...

> ANYWAY

> I ended up writing my own function very similar to fill/drawArc, but with a
> couple of minor changes... This uses the system's pie function to draw the
> entire slice. Here is my source, it differs from fillArc in the fact that it
> removes two lines (the ones disabling the system's pen).

> public void drawPieSlice(GC gc, int x, int y, int width, int height, int
> startAngle, int arcAngle) {
> if (width < 0) {
> x = x + width;
> width = -width;
> }
> if (height < 0) {
> y = y + height;
> height = -height;
> }
> if (width == 0 || height == 0 || arcAngle == 0)
> return;

> int x1, y1, x2, y2, tmp;
> boolean isNegative;
> if (arcAngle >= 360 || arcAngle <= -360) {
> x1 = x2 = x + width;
> y1 = y2 = y + height / 2;
> } else {
> isNegative = arcAngle < 0;

> arcAngle = arcAngle + startAngle;
> if (isNegative) {
> // swap angles
> tmp = startAngle;
> startAngle = arcAngle;
> arcAngle = tmp;
> }
> x1 = Compatibility.cos(startAngle, width / 2) + x + width / 2;
> y1 = -1 * Compatibility.sin(startAngle, height / 2) + y + height
> / 2;

> x2 = Compatibility.cos(arcAngle, width / 2) + x + width / 2;
> y2 = -1 * Compatibility.sin(arcAngle, height / 2) + y + height /
> 2;
> }

> OS.Pie(gc.handle, x, y, x + width + 1, y + height + 1, x1, y1, x2,
> y2);
> }

> Also I've just noted that I removed the winCE support.

> I'm post this code here for 3 reasons.
> 1) People may find it handy if they are doing a similar task
> 2) Someone may want to add it to the GC class
> 3) I might have been stupid at solving problem A, and a existing solution
> exists... If so can someone highlight the error of my ways...

> Thanks
> Andrew

> P.S I noticed a few lines of redundant code in drawArc:

> int nullBrush = OS.GetStockObject(OS.NULL_BRUSH);
> int oldBrush = OS.SelectObject(handle, nullBrush);
> OS.Arc(handle, x, y, x + width + 1, y + height + 1, x1, y1, x2, y2);
> OS.SelectObject(handle, oldBrush);

> Changing the brush has NO effect on Arc method, and as such is not
> needed.... If however the user was using OS.Pie instead then these extra
> lines would be needed.... Since this is not a bug, I don't know whether I
> should add it to the bugs system just to point out that its not needed.
Re: PieSlice - Useful source for people (and maybe inclusion in SWT?) [message #441759 is a reply to message #441751] Tue, 24 August 2004 01:42 Go to previous message
Andrew Brampton is currently offline Andrew BramptonFriend
Messages: 8
Registered: July 2009
Junior Member
Hi,
I did try using Pie for both the drawArc and fillArc, but problem A still
appears.... Also it would be invalid to use Pie inside of drawArc because it
would also draw the two radius lines (which drawArc isn't documented to do,
even though that would be useful)

I suspect that windows is using a different (maybe quicker) algorithm to
fill a bordered area, but when there is no line a different algorithm has to
be used, and thus you get a few pixels of mismatch.

I've submitted the bug report here:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=72480
and created example code (with a image showing the problem)

thanks
Andrew

P.S I've only been using SWT for two weeks and already I've found two
different bugs... The first one was corrected the day after I submitted
it... Keep up the great work guys :)

"Silenio Quarti" <Silenio_Quarti@ca.ibm.com> wrote in message
news:cgdb87$4pl$1@eclipse.org...
> Hi Andrew,
>
> Please open a bug report with this information. I will investigate it.
> Maybe if we called Pie() in both drawArc and fillArc, problem A) would not
> happen. Could you try it? I will check if the null brush is not needed
> anyways.
>
> Thanks!
> Silenio
>
>
>
> Andrew Brampton wrote:
>
>> Hi,
>> I've been trying to draw pie slices for a pie chat. The slice consists of
>> the arc, and two radius length lines...
>
>> I had it drawing fine with fillArc and drawArc, however it had two
>> problems.
>
>> A) The filled area of the slice using fillArc didn't always reach the
>> line
>> drawn by drawArc, there were sometimes a few white background pixels
>> showing
>> at the very edges.
>
>> B) There was no easy way to draw the radius lines.
>
>> I solved problem B by borrowing some code from GC.java and using that to
>> work out the x1,x2,y1,y2 positions of the lines, and then using
>> drawLine(...)
>
>> Problem A I couldn't solve, I figured it could be a mistake in the SWT
>> Source in how it works out the positions, but looking at both fillArc and
>> drawArc showed near identical code, so I don't think its SWT fault, just
>> a
>> (Windows) platform fault...
>
>> ANYWAY
>
>> I ended up writing my own function very similar to fill/drawArc, but with
>> a
>> couple of minor changes... This uses the system's pie function to draw
>> the
>> entire slice. Here is my source, it differs from fillArc in the fact that
>> it
>> removes two lines (the ones disabling the system's pen).
>
>> public void drawPieSlice(GC gc, int x, int y, int width, int height,
>> int
>> startAngle, int arcAngle) {
>> if (width < 0) {
>> x = x + width;
>> width = -width;
>> }
>> if (height < 0) {
>> y = y + height;
>> height = -height;
>> }
>> if (width == 0 || height == 0 || arcAngle == 0)
>> return;
>
>> int x1, y1, x2, y2, tmp;
>> boolean isNegative;
>> if (arcAngle >= 360 || arcAngle <= -360) {
>> x1 = x2 = x + width;
>> y1 = y2 = y + height / 2;
>> } else {
>> isNegative = arcAngle < 0;
>
>> arcAngle = arcAngle + startAngle;
>> if (isNegative) {
>> // swap angles
>> tmp = startAngle;
>> startAngle = arcAngle;
>> arcAngle = tmp;
>> }
>> x1 = Compatibility.cos(startAngle, width / 2) + x + width /
>> 2;
>> y1 = -1 * Compatibility.sin(startAngle, height / 2) + y +
>> height
>> / 2;
>
>> x2 = Compatibility.cos(arcAngle, width / 2) + x + width / 2;
>> y2 = -1 * Compatibility.sin(arcAngle, height / 2) + y +
>> height /
>> 2;
>> }
>
>> OS.Pie(gc.handle, x, y, x + width + 1, y + height + 1, x1, y1,
>> x2,
>> y2);
>> }
>
>> Also I've just noted that I removed the winCE support.
>
>> I'm post this code here for 3 reasons.
>> 1) People may find it handy if they are doing a similar task
>> 2) Someone may want to add it to the GC class
>> 3) I might have been stupid at solving problem A, and a existing solution
>> exists... If so can someone highlight the error of my ways...
>
>> Thanks
>> Andrew
>
>> P.S I noticed a few lines of redundant code in drawArc:
>
>> int nullBrush = OS.GetStockObject(OS.NULL_BRUSH);
>> int oldBrush = OS.SelectObject(handle, nullBrush);
>> OS.Arc(handle, x, y, x + width + 1, y + height + 1, x1, y1, x2, y2);
>> OS.SelectObject(handle, oldBrush);
>
>> Changing the brush has NO effect on Arc method, and as such is not
>> needed.... If however the user was using OS.Pie instead then these extra
>> lines would be needed.... Since this is not a bug, I don't know whether I
>> should add it to the bugs system just to point out that its not needed.
>
>
Previous Topic:taking control over SWT Browser widget
Next Topic:[ CTabFolder ] setting color of deselected (unselected) tabitems
Goto Forum:
  


Current Time: Fri Apr 26 07:12:30 GMT 2024

Powered by FUDForum. Page generated in 0.03473 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top