public InitShape(String str){ this.string=str; float x,y; Scanner sc = new Scanner(string); while (sc.hasNextFloat()) { x=sc.nextFloat(); sc.hasNextFloat(); y=sc.nextFloat(); this.shape.add(new Point(x,y)); x=0; y=0; } } } // CS108 HW1 -- provided simple immutable Point 2-d point class // that encapsulates a double x/y pair. // Could also use the java.awt.Point2D class, but its // interface is more messy.class Point { private double x; private double y;
/** * Constructs a new point. */ public Point(double x, double y) { this.x = x; this.y = y; }
/** * Copy constructor -- copies the given point. * @param other */ public Point(Point other) { this.x = other.x; this.y = other.y; } /** * Gets the x value. * @return x */ public double getX() { return x; } /** * Gets the y value. * @return y */ public double getY() { return y; } /** * Returns a new point which is dx/dy shifted * from this point. * @param dx * @param dy * @return new point dx/dy shifted from this point */ public Point shiftedPoint(double dx, double dy) { return new Point(x+dx, y+dy); }
/** * Returns the distance between this point an another. * @param other * @return distance to other point */ public double distance(Point other) { double x2 = Math.abs(x - other.x); double y2 = Math.abs(y - other.y); return Math.sqrt(x2*x2 + y2*y2); }
/** * Returns a "x y" string representation of the point. * @return string representation */ public String toString() { return x + " " + y; }
/** * Compares two points. Note: uses == on x and y * double values, which is a questionable practice. * Consider using distance() for a more * flexible way to compare two points. */ public boolean equals(Object object) { if (! (object instanceof Point)) return false; Point other = (Point)object; // Note: here we == compare doubles, which is not a good practice return (other.x==x && other.y==y); } } import java.util.LinkedList; class Circle { public Point center; public double radius; private double x=0.0,y=0.0;
Shape data for ShapeClient:
"0 0 0 1 1 1 1 0"
"10 10 10 11 11 11 11 10"
"0.5 0.5 0.5 -10 1.5 0"
"0.5 0.5 0.75 0.75 0.75 0.2"
*/import java.util.Scanner;
public class Shape {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] index={"0 0 0 1 1 1 1 0",
"10 10 10 11 11 11 11 10",
"0.5 0.5 0.5 -10 1.5 0",
"0.5 0.5 0.75 0.75 0.75 0.2"};
DowithShape doshape=new DowithShape(index);
doshape.doit();
}
}
class DowithShape {
private String[] index;
public DowithShape(String[] str){
this.index=str;
}
public void doit(){
InitShape shape[];
shape=new InitShape[index.length];
for(int i=0;i<index.length;i++){
shape[i]=new InitShape(index[i]);
}
Circle circle[];
circle=new Circle[index.length];
for(int j=0;j<index.length;j++){
circle[j]=new Circle(shape[j].shape);
}
for(int i=1;i<index.length;i++){
System.out.println("a crosses "+((char)((int)'a'+i))+": "+circle[0].isCrossCircle(shape[i]));
}
for(int i=1;i<index.length;i++){
System.out.println("a encircle "+((char)((int)'a'+i))+": "+circle[0].isEncircle(circle[i]));
}
}
}
class InitShape {
private String string;
public LinkedList<Point> shape=new LinkedList<Point>();
public InitShape(String str){
this.string=str;
float x,y;
Scanner sc = new Scanner(string);
while (sc.hasNextFloat()) {
x=sc.nextFloat();
sc.hasNextFloat();
y=sc.nextFloat();
this.shape.add(new Point(x,y));
x=0;
y=0;
}
}
}
// CS108 HW1 -- provided simple immutable Point 2-d point class
// that encapsulates a double x/y pair.
// Could also use the java.awt.Point2D class, but its
// interface is more messy.class Point {
private double x;
private double y;
/**
* Constructs a new point.
*/
public Point(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Copy constructor -- copies the given point.
* @param other
*/
public Point(Point other) {
this.x = other.x;
this.y = other.y;
} /**
* Gets the x value.
* @return x
*/
public double getX() {
return x;
}
/**
* Gets the y value.
* @return y
*/
public double getY() {
return y;
}
/**
* Returns a new point which is dx/dy shifted
* from this point.
* @param dx
* @param dy
* @return new point dx/dy shifted from this point
*/
public Point shiftedPoint(double dx, double dy) {
return new Point(x+dx, y+dy);
}
/**
* Returns the distance between this point an another.
* @param other
* @return distance to other point
*/
public double distance(Point other) {
double x2 = Math.abs(x - other.x);
double y2 = Math.abs(y - other.y);
return Math.sqrt(x2*x2 + y2*y2);
}
/**
* Returns a "x y" string representation of the point.
* @return string representation
*/
public String toString() {
return x + " " + y;
}
/**
* Compares two points. Note: uses == on x and y
* double values, which is a questionable practice.
* Consider using distance() for a more
* flexible way to compare two points.
*/
public boolean equals(Object object) {
if (! (object instanceof Point)) return false;
Point other = (Point)object;
// Note: here we == compare doubles, which is not a good practice
return (other.x==x && other.y==y);
}
}
import java.util.LinkedList; class Circle {
public Point center;
public double radius;
private double x=0.0,y=0.0;
public Circle(LinkedList<Point> shape){
for(Point a:shape){
x=x+a.getX();
y=y+a.getY();
}
this.center=new Point(x/shape.size(),y/shape.size());
double radiusflag=center.distance(shape.get(0));
for(Point a:shape){
if(radiusflag>center.distance(a))radiusflag=center.distance(a);
}
this.radius=radiusflag;
}
public boolean isCrossCircle(InitShape other){
double min_distance=0,max_distance=0;
//Point nearest_point=new Point(other.shape.get(0));
int now,nex;
double o=0,b=0,c=0;
for(Point a:other.shape){
if(other.shape.indexOf(a)==0) {
min_distance=center.distance(a);
max_distance=center.distance(a);
}
if(center.distance(a)<min_distance) min_distance=center.distance(a);
if(center.distance(a)>max_distance) max_distance=center.distance(a);
}
if((!(min_distance>radius))&&max_distance>radius)return true;
for(Point a:other.shape){
if(other.shape.indexOf(a)==0) {
max_distance=center.distance(a);
}
if(center.distance(a)>max_distance) max_distance=center.distance(a);
now=other.shape.indexOf(a);
if(now==other.shape.size()-1)nex=0;
else nex=now+1;
o=center.distance(a);
b=a.distance(other.shape.get(nex));
c=center.distance(other.shape.get(nex));
}
if(getHight(o, b, c)<radius&&isNotObtuse(o,b,c)&&max_distance>radius)return true;
return false;
}
public double getHight(double a,double b,double c){
double high,s;
s=(a+b+c)/2;
high=Math.sqrt(s*(s-a)*(s-b)*(s-c));
return high;
}
public boolean isNotObtuse(double a,double b,double c){
return !((a*a+b*b-c*c)<0);
}
public int isEncircle(Circle other){
if((center.distance(other.center))<radius||(center.distance(other.center))==radius) return 2;
if(center.distance(other.center)<radius+other.radius) return 1;
else return 0;
}
}