Add some gemometry point classes in math\geometry package and standard draw libaray

This commit is contained in:
2014-03-15 10:34:20 +01:00
parent a3762a99e6
commit c0033f60be
6 changed files with 1598 additions and 0 deletions

View File

@@ -0,0 +1,194 @@
package math.geometry;
import java.text.DecimalFormat;
import math.matrix.Vector;
public class Point {
protected double[] point;
protected int n;
/**
* empty point.
*/
public Point(int n){
this.n = n;
point = new double[n];
}
/**
* new point.
* @param x x-coordinate
* @param y y-coordinate
* @param z z-coordinate
*/
public Point(double x, double y, double z){
n = 3;
point = new double[]{x, y, z};
}
/**
* new point.
* @param p n-dimensional point
*/
public Point(double... p){
n = p.length;
point = p;
}
/**
* Copy constructor.
* @param a the point to copy
*/
public Point(Point a){
this(a.get());
}
public Point create(int n){
if(this instanceof Point2D) return new Point2D();
else return new Point(n);
}
/**
* Create random point.
*/
public static Point random(int n){
double[] point = new double[n];
int i;
for(i=0; i<n; i++)
point[i] = Math.random();
return new Point(point);
}
/**
* Get coordinates.
*/
public double[] get(){ return point; }
/**
* Get coordinate.
* @param i index of coordinate (first: 0)
*/
public double get(int i){ return point[i]; }
/**
* Set coordinate.
* @param i index of coordinate (first: 0)
* @param a value
*/
public void set(int i, double a){ point[i] = a; }
/**
* Get x-coordinate.
* @return x-coordinate
*/
public double x(){ return get(0); }
/**
* Get y-coordinate.
* @return y-coordinate
*/
public double y(){ return get(1); }
/**
* Get z-coordinate.
* @return z-coordinate
*/
public double z(){ return get(2); }
/**
* Get dimension.
* @return dimansion
*/
public int n(){ return n; }
/**
* Get distance between origin and point.
* @return distance
*/
public double r(){
int i;
double squares = 0;
for(i=0; i<n; i++)
squares += (point[i]*get(i));
return Math.sqrt(squares);
}
public double theta3d(){ return Math.atan2(y(), x()); }
public double phi3d(){ return Math.acos(z()/r()); }
/**
* Displace point.
* @param a the displacement vector
* @return moved point
*/
public Point displace(Vector a){
int i;
Point point = create(n);
for(i=0; i<n; i++)
point.set(i,get(i)+a.get(i));
return point;
}
/**
* Displacement vector between this point and another one.
* @param a other point
* @return displacement vector
*/
public Vector displace(Point a){
int i;
double[] point = new double[n];
for(i=0; i<n; i++)
point[i] = a.get(i)-get(i);
return new Vector(point);
}
/**
* Euclidean distance between this point and another.
* @param a other point
* @return distance
*/
public double distance(Point a){
return displace(a).norm();
}
/**
* Euclidean distance between this point and another.
* @param x component of other point
* @param y component of other point
* @return distance
*/
public double distance(double x, double y){
return displace(new Point(x, y)).norm();
}
public boolean equals(Object obj){
int i;
Point point = (Point) obj;
for(i=0; i<n; i++)
if(get(i) != point.get(i)) return false;
return true;
}
/**
* String of the point.
* Format: (x1;x2;...;xn)
*/
@Override
public String toString(){
int i;
DecimalFormat df = new DecimalFormat("#.##");
String string = "(";
for(i=0; i<n; i++){
string += i==0?"":"; ";
string += df.format(get(i));
}
string += ")";
return string;
}
public void println(){
System.out.println(this);
}
}

View File

@@ -0,0 +1,84 @@
package math.geometry;
import math.Maths;
/**
* Work with 2D point using rectangular coordinates.
* @author Daniel Weschke
*/
public class Point2D extends Point {
/**
* null point.
*/
public Point2D(){
super(2);
}
/**
* (x,y)-point.
* @param a x-coordinate
* @param b y-coordinate
*/
public Point2D(double a, double b){
super(a, b);
}
public Point2D(Point2D p){
super(p);
}
/**
* Create random point.
*/
public static Point2D random(){
return new Point2D(Math.random(), Math.random());
}
/**
* Get angle between first axis (x) and the line from origin to the point.
* @return angle
*/
public double theta(){ return Math.atan2(y(), x()); }
/**
* Set x-coordinate.
* @param a x-coordinate
*/
public void setX(double a){ set(0, a); }
/**
* set y-coordinate.
* @param b y-coordinate
*/
public void setY(double b){ set(1, b); }
public static Point2D polarToCartesianD(double length, double angle){
return new Point2D(length*Maths.cosd(angle), length*Maths.sind(angle));
}
public static double[] cartesianToPolar(double x, double y){
return new double[]{
Maths.hypot(x, y),
Math.atan2(y, x)
};
}
/**
* test client
* @param args
*/
public static void main(String[] args) {
Point2D p = Point2D.random();
System.out.println("p = " + p);
System.out.println(" x = " + p.x());
System.out.println(" y = " + p.y());
System.out.println(" r = " + p.r());
System.out.println(" theta = " + p.theta());
System.out.println();
Point2D q = new Point2D(0.5, 0.5);
System.out.println("q = " + q);
System.out.println("dist(p, q) = " + p.distance(q));
}
}

View File

@@ -0,0 +1,11 @@
package math.geometry;
public class PointPolar {
double r;
double theta;
double phi;
public double x(){ return r*Math.cos(theta)*Math.sin(phi); }
public double y(){ return r*Math.sin(theta)*Math.sin(phi); }
public double z(){ return r*Math.cos(phi); }
}

View File

@@ -0,0 +1,57 @@
package math.geometry;
/*************************************************************************
* Compilation: javac PointPolar.java
* Execution: java PointPolar
*
* Implementation of 2D point using polar coordinates.
*
*************************************************************************/
public final class PointPolar2D {
private final double r;
private final double theta;
// constructor
PointPolar2D() {
double x = Math.random();
double y = Math.random();
r = Math.sqrt(x*x + y*y);
theta = Math.atan2(y, x);
}
PointPolar2D(double x, double y) {
r = Math.sqrt(x*x + y*y);
theta = Math.atan2(y, x);
}
double r() { return r; }
double theta() { return theta; }
double x() { return r*Math.cos(theta); }
double y() { return r*Math.sin(theta); }
double distanceTo(PointPolar2D that) {
double dx = this.x() - that.x();
double dy = this.y() - that.y();
return Math.sqrt(dx*dx + dy*dy);
}
public String toString() { return "(" + x() + ", " + y() + ")"; }
// test client
public static void main(String[] args) {
PointPolar2D p = new PointPolar2D();
System.out.println("p = " + p);
System.out.println(" x = " + p.x());
System.out.println(" y = " + p.y());
System.out.println(" r = " + p.r());
System.out.println(" theta = " + p.theta());
System.out.println();
PointPolar2D q = new PointPolar2D(0.5, 0.5);
System.out.println("q = " + q);
System.out.println("dist(p, q) = " + p.distanceTo(q));
}
}

105
src/stdlib/Color.java Normal file
View File

@@ -0,0 +1,105 @@
package stdlib;
public class Color extends java.awt.Color{
/**
* UID
*/
private static final long serialVersionUID = -6866051674205095467L;
// Color from java.awt.Color
public static final Color WHITE = new Color(255, 255, 255);
public static final Color LIGHT_GRAY = new Color(192, 192, 192);
public static final Color GRAY = new Color(128, 128, 128);
public static final Color DARK_GRAY = new Color( 64, 64, 64);
public static final Color BLACK = new Color( 0, 0, 0);
public static final Color RED = new Color(255, 0, 0);
public static final Color PINK = new Color(255, 175, 175);
public static final Color ORANGE = new Color(255, 200, 0);
public static final Color YELLOW = new Color(255, 255, 0);
public static final Color GREEN = new Color( 0, 255, 0);
public static final Color MAGENTA = new Color(255, 0, 255);
public static final Color CYAN = new Color( 0, 255, 255);
public static final Color BLUE = new Color( 0, 0, 255);
public static final Color DIM_GRAY = new Color(105, 105, 105);
public static final Color JET = new Color( 52, 52, 52);
public static final Color BROWN = new Color(165, 42, 42);
public static final Color DARK_RED = new Color(139, 0, 0);
public static final Color DEEPSKYBLUE = new Color( 0, 191, 255);
public static final Color PURPLE = new Color(160, 32, 240);
public static final Color BLUE_GREEN = new Color( 9, 90, 166);
public static final Color LIGHT_BLUE = new Color(103, 198, 243);
public static final Color LIGHT_RED = new Color(150, 35, 31);
public Color(int rgb){
super(rgb);
}
public Color(int r, int g, int b) {
super(r, g, b);
}
public Color(float h, float s, float b){
super(h, s, b);
}
public Color(int i, int j, int k, int l) {
super(i, j, k, l);
}
public static Color getHSBColor(float h, float s, float b) {
return new Color(HSBtoRGB(h, s, b));
}
public static int HSBtoRGB(float hue, float saturation, float brightness) {
int r = 0, g = 0, b = 0;
if (saturation == 0) {
r = g = b = (int) (brightness * 255.0f + 0.5f);
} else {
float h = (hue - (float)Math.floor(hue)) * 6.0f;
float f = h - (float)java.lang.Math.floor(h);
float p = brightness * (1.0f - saturation);
float q = brightness * (1.0f - saturation * f);
float t = brightness * (1.0f - (saturation * (1.0f - f)));
switch ((int) h) {
case 0:
r = (int) (brightness * 255.0f + 0.5f);
g = (int) (t * 255.0f + 0.5f);
b = (int) (p * 255.0f + 0.5f);
break;
case 1:
r = (int) (q * 255.0f + 0.5f);
g = (int) (brightness * 255.0f + 0.5f);
b = (int) (p * 255.0f + 0.5f);
break;
case 2:
r = (int) (p * 255.0f + 0.5f);
g = (int) (brightness * 255.0f + 0.5f);
b = (int) (t * 255.0f + 0.5f);
break;
case 3:
r = (int) (p * 255.0f + 0.5f);
g = (int) (q * 255.0f + 0.5f);
b = (int) (brightness * 255.0f + 0.5f);
break;
case 4:
r = (int) (t * 255.0f + 0.5f);
g = (int) (p * 255.0f + 0.5f);
b = (int) (brightness * 255.0f + 0.5f);
break;
case 5:
r = (int) (brightness * 255.0f + 0.5f);
g = (int) (p * 255.0f + 0.5f);
b = (int) (q * 255.0f + 0.5f);
break;
}
}
return 0xff000000 | (r << 16) | (g << 8) | (b << 0);
}
}

1147
src/stdlib/StdDraw.java Normal file

File diff suppressed because it is too large Load Diff