Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld >> Java Beginner

Pages: 1
LJB
stranger


Reged: 02/13/05
Posts: 2
Circles
      #15495 - 02/13/05 07:29 AM

I have written the following three classes. I was wondering if anyone could suggest what method I should write
to find whether the first and last (non-singular) circles in the set overlap, touch or are disjoint. The result of the test should be stored in the public variable posFirstLast of the class Project1. The value should be 0 if two circles are disjoint, 1 if they overlap, 2 if they tuch and 3 if they are identical.

Thank you for your help.
-------------------------
public class Project1 {
public int circleCounter, posFirstLast;
public double maxArea, minArea, averageArea, stdArea, medArea;
public int stamp = 773725;
public Project1() {
}
public Circle[] sort(Circle[] circles) {
for (int i = 0; i < (circles.length - 1); i++) {
int minpos = 0;
double temparea = circles.area();
for (int j = (i + 1); j < circles.length; j++) {
if (circles[j].area() < temparea) {
temparea = circles[j].area();
minpos = j;
}
}
if (minpos != i) {
Circle swaptemp = circles;
circles = circles[minpos];
circles[minpos] = swaptemp;
}
}
return circles;
}
public void Results(String data) {
// read data in
int n = circleCounter;
Circle[] circles = new Circle[n];
for (int i = 0; i < n; i++) {
//read in data
}
double tempSumArea, tempSumSquareArea, tempMaxArea, tempMinArea;
tempSumArea = tempSumSquareArea = tempMaxArea = tempMinArea = 0;
Circle zeroCircle = new Circle(0, 0, 0);
int numOfZeroCircles = 0;
for (int i = 0; i < circles.length; i++) {
if (!zeroCircle.equals(circles)) {
circleCounter++;
tempSumArea = tempSumArea + circles.area();
tempSumSquareArea = tempSumSquareArea
+ (circles.area() * circles.area());
if (circles.area() > tempMaxArea) {
tempMaxArea = circles.area();
} else if (circles.area() < tempMinArea) {
tempMinArea = circles.area();
}
} else {
numOfZeroCircles++;
}
}
maxArea = tempMaxArea;
minArea = tempMinArea;
int adjustedN = circles.length - numOfZeroCircles;
averageArea = (tempSumArea / adjustedN);
stdArea = Math.sqrt((tempSumSquareArea - (tempSumArea * tempSumArea))
/ (adjustedN * (adjustedN - 1)));
circles = sort(circles);
int len = circles.length;
if ((len % 2) == 0) {
medArea = (circles[(len / 2) - 2].area() + circles[(len / 2) - 1]
.area()) / 2;
} else {
medArea = circles[(len / 2) - 1].area();
}
}
}
-------------------------
public class Circle {
private Point center;
private double radius;
public Circle() {
}
public Circle (double xc,double yc, double rad) {
setCenter(xc,yc);
this.radius = rad;
}
public Circle (Point center, double rad) {
setCenter(center);
this.radius = rad;
}
public void setCenter (double xc, double yc) {
this.center = new Point(xc, yc);
}
public void setCenter(Point a) {
this.center = a;
}
public void setRadius(double rad) {
this.radius = rad;
}
public Point getCenter() {
return center;
}
public double getRadius() {
return radius;
}
public String toString() {
return "[ Center=" + this.center.toString() +
" Radius=" + this.radius + "]";
}
public boolean equals(Object obj) {
if (obj instanceof Circle) {
boolean test = false;
Circle c = (Circle)obj;
test = this.getCenter().equals(c.getCenter());
test = test && (c.getRadius() == this.radius);
return test;
} else {
return false;
}
}
public double area() {
return (Math.PI * radius * radius);
}
public int overlap(Circle c) {
Point center2 = c.getCenter();
double radius2 = c.getRadius();
double cdist = center.distance(center2);
double sumradius = radius + radius2;
if (cdist > sumradius) {
return 0;
} else if (cdist < sumradius) {
return 1;
} else if (cdist == sumradius) {
return 2;
} else {
return 3;
}
}
public static void main (String[] args) {
}
}
-------------------------
public class Point {
private double x,y;
public static final double GEOMTOL=1.0e-6;
public Point() {
setPoint(0.0,0.0);
}
public Point (double x, double y) {
setPoint(x,y);
}
public void setPoint(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public String toString() {
return "[" + x + "," + y + "]";
}
public double distance(Point p) {
double distance = ((this.x - p.x) * (this.x - p.x))
+ ((this.y - p.y) * (this.y - p.y));
return (Math.sqrt(distance));
}
public boolean equals(Object obj) {
if (obj instanceof Point) {
Point q = (Point)obj;
return (Math.abs(x - q.x) <= GEOMTOL &&
Math.abs(y - q.y) <= GEOMTOL);
} else {
return false;
}
}
/**
* @param args
*/
public static void main (String[] args) {
}
}


Post Extras: Print Post   Remind Me!   Notify Moderator  
hiwa
Carpal Tunnel


Reged: 06/21/03
Posts: 7704
Loc: Japan
Re: Circles [Re: LJB]
      #15510 - 02/13/05 10:24 PM

A high school math exercise. If I had plenty of time to kill ...


--------------------
*stop cruelty* Annual number of euthanized cats&dogs: US 5M, JP 500K.*for our better karma*


Post Extras: Print Post   Remind Me!   Notify Moderator  
LJB
stranger


Reged: 02/13/05
Posts: 2
Re: Circles [Re: hiwa]
      #15521 - 02/14/05 10:23 AM

It may be easy for you but I have only been programming for 6 weeks and so would appreciate some help!!

Thanks


Post Extras: Print Post   Remind Me!   Notify Moderator  
hiwa
Carpal Tunnel


Reged: 06/21/03
Posts: 7704
Loc: Japan
Re: Circles [Re: LJB]
      #15547 - 02/15/05 04:35 AM

> circles in the set overlap, touch or are disjoint
This is not a Java programming issue.
Consider circle A and circle B.
The radius of A is ra and the radius of B is rb.
The center of A is (xa, ya) and the center of B is (xb, yb).
You can draw a line from (xa, ya) to (xb, yb).
If the length of the line == ra + rb, A and B touches.
If the length of the line > ra + rb, A and B didjoints.
If the length of the line < ra + rb, A and B overlaps.



--------------------
*stop cruelty* Annual number of euthanized cats&dogs: US 5M, JP 500K.*for our better karma*


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 19 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 2024

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5