KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > math > MathUtil


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on May 17, 2005
33  */

34 package com.nightlabs.math;
35
36 import java.awt.geom.Point2D JavaDoc;
37
38 /**
39  * @author Marco Schulze - marco at nightlabs dot de
40  */

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     /**
54      * calculates a circle which outline contains all three given parameters
55      *
56      * @param point1 an arbitrarily Point
57      * @param point2 an arbitrarily Point
58      * @param point3 an arbitrarily Point
59      * @return a Ccrcle which outline contains all three parameters point1, point2, point3
60      */

61     public static Circle getCircle(Point2D JavaDoc point1, Point2D JavaDoc point2, Point2D JavaDoc point3)
62     {
63       // Alternative Arc2D.setArcByTangent(Point2D p1,Point2D p2,Point2D p3,double radius)
64
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     /**
78      * Calculates the rotation in degrees depending on a certain point (mainly the mousePosition)
79      * and another fix point
80      *
81      * @param mouseX X-Coordinate of the mouse
82      * @param mouseY Y-Coordinate of the mouse
83      * @param centerX X-Coordinate of a fix point
84      * @param centerY Y-Coordinate of a fix point
85      * @return the rotation (angle) of the line described through two points given by (mouseX, mouseY) and (centerX, centerY)
86      */

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   /**
106    *
107    * @param x1 x-coordinate of first Point
108    * @param y1 y-coordinate of first Point
109    * @param x2 x-coordinate of second Point
110    * @param y2 y-coordinate of second Point
111    * @param d the distance from first Point
112    * @return the Point on the Line which goes through both points (x1,x2) (y1,y2) with the distance d from first Point
113    */

114   public static Point2D JavaDoc 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 JavaDoc(x, y);
130   }
131 // public static Point2D getPointOnLineWithDistance(double x1, double y1, double x2, double y2, double d)
132
// {
133
// double x = x1 + (d*(x1-x2) / Math.sqrt( (Math.pow(x1-x2,2)) + (Math.pow(y1-y2,2)) ));
134
// double y = x2 + (d*(y1-y2) / Math.sqrt( (Math.pow(x1-x2,2)) + (Math.pow(y1-y2,2)) ));
135
// return new Point2D.Double(x, y);
136
// }
137

138   /**
139    *
140    * @param p1 first Point
141    * @param p2 second Point
142    * @param d the distance from p1
143    * @return @see getPointOnLineWithDistance(double x1, double y1, double x2, double y2, double d)
144    */

145   public static Point2D JavaDoc getPointOnLineWithDistance(Point2D JavaDoc p1, Point2D JavaDoc p2, double d)
146   {
147     return getPointOnLineWithDistance(p1.getX(), p1.getY(), p2.getX(), p2.getY(), d);
148   }
149   
150 }
151
Popular Tags