Centering Shapes?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Alex

Centering Shapes?

Post by Alex » Tue Feb 25, 2003 7:02 pm

Hi Everyone,

How can I make sure the following shapes are centered around (0,0)?
I have tried various combinations but they still appear off centered
when I plot them in a chart.

private Shape generateShape(int aColorIndex)
{
double lScale = 10;
double lDelta = .5 * lScale;
int lIndex = aColorIndex % 10;
int[] lXPoints = null;
int[] lYPoints = null;

int lXPoint = 0;
int lYPoint = 0;
switch (lIndex)
{
case 0:
// Square
return new Rectangle2D.Double(0,0,lScale, lScale);
case 1:
// Circle
return new Ellipse2D.Double(0,0,lScale, lScale);
case 2:
// Up-pointing triangle
lXPoints = intArray(lXPoint, lXPoint + lDelta, lXPoint - lDelta);
lYPoints = intArray(lYPoint- lDelta, lYPoint + lDelta, lYPoint + lDelta);
return new Polygon(lXPoints, lYPoints, 3);
case 3:
// Diamond
lXPoints = intArray(lXPoint, lXPoint + lDelta, lXPoint , lXPoint - lDelta);
lYPoints = intArray(lYPoint - lDelta, lYPoint, lYPoint + lDelta, lYPoint);
return new Polygon(lXPoints, lYPoints, 4);
case 4:
// Horizontal rectangle
return new Rectangle2D.Double(lXPoint - lDelta, lYPoint - lDelta / 2, lScale, lScale / 2);
case 5:
// Down-pointing triangle
lXPoints = intArray(lXPoint - lDelta, lXPoint + lDelta, lXPoint);
lYPoints = intArray(lYPoint - lDelta, lYPoint - lDelta, lYPoint + lDelta);
return new Polygon(lXPoints, lYPoints, 3);
case 6:
// Horizontal ellipse
return new Ellipse2D.Double(lXPoint - lDelta, lYPoint - lDelta / 2, lScale, lScale / 2);
case 7:
// Right-pointing triangle
lXPoints = intArray(lXPoint - lDelta, lXPoint + lDelta, lXPoint - lDelta);
lYPoints = intArray(lYPoint - lDelta, lYPoint , lYPoint + lDelta);
return new Polygon(lXPoints, lYPoints, 3);
case 8:
// Vertical rectangle
return new Rectangle2D.Double(lXPoint - lDelta / 2, lYPoint - lDelta, lScale / 2, lScale);
default:
// Vertical ellipse
return new Ellipse2D.Double(lXPoint - lDelta / 2, lYPoint - lDelta, lScale / 2, lScale);
}

}

David Gilbert

Re: Centering Shapes?

Post by David Gilbert » Wed Feb 26, 2003 12:23 am

Hi Alex,

The Shape objects that you create should have their centres at (0, 0), because JFreeChart just translates the coordinates to (x, y) as required.

For example, to create a square 6 units across:

return new Rectangle2D.Double(-3.0, -3.0, 6.0, 6.0);

Regards,

Dave Gilbert

Alex

Re: Centering Shapes?

Post by Alex » Wed Feb 26, 2003 2:23 am

Thanks David I don't know what I was thinking....

As always your help is greatly appreciated!!!

Alex

Locked