Algorithm for Plotting Objects on Ellipse

Hello All,

I am sure something like this has been posted before, but I could not find it via search.

I am wondering how I can go about plotting nodes or objects on an ellipse. I have code for a circle. The circle algorithm takes into account the number of nodes in the list and calculates the number of radians between each node.

I would like something similiar for plotting these nodes on an ellipse. It would help with filling in the empty space of the window.

Here is my algorithm for circle plotting (drawing is done in another area, this only sets the location):

/**

* Moves the position of the nodes into a circular layout using the specified dimension as the bounds.

* @param dim The height and width to bound the positions of the nodes.

*/

publicvoid layoutNodes(Dimension dim){

int startX = dim.width / 2 ;

int startY = dim.height / 2;

double borderFactor = 0.8;// keeps the radius down to 80% of the actual height or width

double radius = Math.min(dim.width, dim.height) / 2.0 * borderFactor;

double theta = 0.0;

int x1, y1;

// determine the number of radians to move per node in the list.

double radiansPerVertex = (360.0 * deg2rad / m_nodeList.size());

Iterator itAllNodes = m_nodeList.iterator();

Point pv =new Point();

while (itAllNodes.hasNext()){

Node node = ((Node) itAllNodes.next());

// copy the node's old location into the point for working purposes

pv = node.getLocation(pv);

// convert from polar to cartesian coords

x1 = (int) (radius * Math.cos(theta));

y1 = (int) (radius * Math.sin(theta));

pv.x = startX + x1 - 20;// 20 is just a fudge factor for centering purposes.

pv.y = startY + y1;

// now move the node to the new location.

node.setLocation(pv);

// move to the next mark in the circle

theta += radiansPerVertex;

}

}

Thank you for any help.

[2780 byte] By [nbloma] at [2007-10-3 3:54:37]
# 1

instead of calculating a single radius which you use in your x and y calculation, calculate two of them.

double xRadius = dim.width/2.0*borderFactor;

double yRadius = dim.height/2.0*borderFactor;

then use the appropriate radius for the appropriate coord.

x1 = (int) (xRadius * Math.cos(theta));

y1 = (int) (yRadius * Math.sin(theta));

marlin314a at 2007-7-14 21:52:36 > top of Java-index,Other Topics,Algorithms...
# 2
Thank you, worked like a charm.
nbloma at 2007-7-14 21:52:36 > top of Java-index,Other Topics,Algorithms...