1 30 31 34 package com.nightlabs.math; 35 36 import java.awt.geom.Point2D ; 37 38 41 public class MathUtil 42 { 43 44 protected MathUtil() 45 { 46 } 47 48 public static double log(double base, double value) 49 { 50 return Math.log(value) / Math.log(base); 51 } 52 53 61 public static Circle getCircle(Point2D point1, Point2D point2, Point2D point3) 62 { 63 double a,b,c,d,e,f; 65 a=point1.getX(); 66 b=point1.getY(); 67 c=point2.getX(); 68 d=point2.getY(); 69 e=point3.getX(); 70 f=point3.getY(); 71 double middleX = ((Math.pow(a,2)+Math.pow(b,2))*(f-d) + (Math.pow(c,2)+Math.pow(d,2))*(b-f) + (Math.pow(e,2)+Math.pow(f,2))*(d-b)) / (2*(a*(f-d)+c*(b-f)+e*(d-b))); 72 double middleY = ((Math.pow(a,2)+Math.pow(b,2))*(e-c) + (Math.pow(c,2)+Math.pow(d,2))*(a-e) + (Math.pow(e,2)+Math.pow(f,2))*(c-a)) / (2*(b*(e-c)+d*(a-e)+f*(c-a))); 73 double radius = Math.sqrt(Math.pow(a-middleX,2) + Math.pow(b-middleY,2)); 74 return new Circle(middleX, middleY, radius); 75 } 76 77 87 public static double calcRotation(double mouseX, double mouseY, double centerX, double centerY) 88 { 89 double rotation = 0; 90 if (mouseX > centerX && mouseY < centerY) { 91 rotation = 360-180 * Math.atan((mouseX-centerX)/(centerY-mouseY))/Math.PI; 92 } 93 else if (mouseX < centerX && mouseY < centerY) { 94 rotation = 180 * Math.atan((centerX-mouseX)/(centerY-mouseY))/Math.PI; 95 } 96 else if (mouseX < centerX && mouseY > centerY) { 97 rotation = 90 + 180 * Math.atan((mouseY-centerY)/(centerX-mouseX))/Math.PI; 98 } 99 else if (mouseX > centerX && mouseY > centerY) { 100 rotation = 180 + 180 * Math.atan((mouseX-centerX)/(mouseY-centerY))/Math.PI; 101 } 102 return rotation; 103 } 104 105 114 public static Point2D getPointOnLineWithDistance(double x1, double y1, double x2, double y2, double d) 115 { 116 double x = 0; 117 double y = 0; 118 119 if (x1 == x2) 120 x = x1; 121 else 122 x = x1 + (d*(x1-x2) / Math.sqrt( (Math.pow(x1-x2,2)) + (Math.pow(y1-y2,2)) )); 123 124 if (y1 == y2) 125 y = y1; 126 else 127 y = x2 + (d*(y1-y2) / Math.sqrt( (Math.pow(x1-x2,2)) + (Math.pow(y1-y2,2)) )); 128 129 return new Point2D.Double (x, y); 130 } 131 138 145 public static Point2D getPointOnLineWithDistance(Point2D p1, Point2D p2, double d) 146 { 147 return getPointOnLineWithDistance(p1.getX(), p1.getY(), p2.getX(), p2.getY(), d); 148 } 149 150 } 151 | Popular Tags |