java


Angle between 3 points signed, bad results


I want to develop a kind of wheel of fortune in Java/Android. When the user touch the screen, i detect the mouvement and for each changes, i calculate the angle between the old pressure et the newest (function onScroll). I've a problem because i don't remember how i can calculate the angle between 3 points...
I develop 3 functions but each one give me a various result :
public class Test {
public static void main(String[] args) {
Test test = new Test();
Point center = new Point(2.26f, 2.26f);
Point current = new Point(2.54f, 3.64f);
Point previous = new Point(2.25f, 3.73f);
System.out.println("1) Angle is "
+ test.function1(center, current, previous));
System.out.println("2) Angle is "
+ test.function2(center, current, previous));
System.out.println("3) Angle is "
+ test.function3(center, current, previous));
System.out.println("################################");
center = new Point(2.26f, 2.26f);
previous = new Point(3.29f, 1.04f);
current = new Point(0.98f, 2.25f);
System.out.println("1) Angle is "
+ test.function1(center, current, previous));
System.out.println("2) Angle is "
+ test.function2(center, current, previous));
System.out.println("3) Angle is "
+ test.function3(center, current, previous));
System.out.println("################################");
center = new Point(226.0f, 226.0f);
previous = new Point(225.21994f, 373.3158f);
current = new Point(254.31085f, 364.05264f);
System.out.println("1) Angle is "
+ test.function1(center, current, previous));
System.out.println("2) Angle is "
+ test.function2(center, current, previous));
System.out.println("3) Angle is "
+ test.function3(center, current, previous));
System.out.println("################################");
}
public double function1(Point center, Point current, Point previous) {
double ang1 = Math.atan((previous.getdY() - center.getdY())
/ (previous.getdX() - center.getdX()));
double ang2 = Math.atan((current.getdY() - center.getdY())
/ (current.getdX() - center.getdX()));
double rslt = ang1 - ang2;
return Math.toDegrees(rslt) * -1;
}
private double function2(Point center, Point current, Point previous) {
float dx = current.getdX() - center.getdX();
float dy = current.getdY() - center.getdY();
double a = Math.atan2(dy, dx);
float dpx = previous.getdX() - center.getdX();
float dpy = previous.getdY() - center.getdY();
double b = Math.atan2(dpy, dpx);
double diff = a - b;
double degres = Math.toDegrees(diff);
return degres;
}
public double function3(Point center, Point current, Point previous) {
Point p1 = new Point(current.getdX() - center.getdX(), current.getdY()
- center.getdY());
Point p2 = new Point(previous.getdX() - center.getdX(),
previous.getdY() - previous.getdY());
double angle = Math.atan2(p1.getdY() - p2.getdY(),
p1.getdX() - p2.getdX());
return Math.toDegrees(angle);
}
}
I found this function on the net but i can't know which is the best.
Can you help me ?
Create two vectors out of the points and use dot product.
private double angleBetween(Point center, Point current, Point previous) {
return Math.toDegrees(Math.atan2(current.x - center.x,current.y - center.y)-
Math.atan2(previous.x- center.x,previous.y- center.y));
}
this first calculates the angle of center->current and center->previous against the x-axis and takes the difference between the 2
this is similar to function2
Have a look here: http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
In terms of your function2:
private double function2(Point center, Point current, Point previous) {
float v1x = current.getdX() - center.getdX();
float v1y = current.getdY() - center.getdY();
//need to normalize:
float l1 = Math.sqrt(v1x * v1x + v1y * v1y);
v1x /= l1;
v1y /= l1;
float v2x = previous.getdX() - center.getdX();
float v2y = previous.getdY() - center.getdY();
//need to normalize:
float l2 = Math.sqrt(v2x * v2x + v2y * v2y);
v2x /= l2;
v2y /= l2;
double rad = Math.acos( v1x * v2x + v1y * v2y );
double degres = Math.toDegrees(rad);
return degres;
}
Edit: for signed values use Math.atan2(...).
Quote from the linked page:
If we want a + or - value to indicate which vector is ahead, then we probably need to use the atan2 function (as explained on this page). using:
angle of 2 relative to 1= atan2(v2.y,v2.x) - atan2(v1.y,v1.x)
Thus replace double rad = Math.acos( v1x * v2x + v1y * v2y ); with double rad = Math.atan2( v2y,v2x) - Math.atan2(v1y,v1x); and you should be fine.

Related Links

How to get data from an intent fired from an adapter?
what is the mistake in this code (BST)
Creating a Stack with an internal array
NumberFormatException message doesnt change?
Jersey Server running using a runnable jar
Generate html form from wsdl
Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/hibernateContext.xml]
How to automatically write and read from files?
how can we save result of Command window on file, MATLAB
JComponent isn't setting position nor text
#Pattern annotation for a method parameter
Set the time-of-day on a ZonedDateTime in java.time?
Why java.lang.NoSuchMethodError when dynamically load a dex jar?
Elastic Search - How to perform IN condition in elastic search
SB - How to provide a hibernate.cfg.xml file to a LocalSessionFactoryBuilder?
Query list based on input list value

Categories

HOME
arduino-uno
google-chrome-extension
jdo
cookies
lodash
bpmn
react-router
framework7
graphql
jpeg
installshield
ravendb
indesign
django-imagekit
ios-charts
bs4
cloudkit
dax
rascal
export-to-csv
correlation
timeout
orchardcms
ibm-odm
decomposition
decimal
tostring
google-cloud-speech
conemu
paging
sox
dcevm
spark-jobserver
uninstall
applozic
kendo-datasource
lightswitch-2013
bluestacks
google-rich-snippets
bosh
yadcf
key-value-observing
phonegap
google-cloud-endpoints-v2
twilio-api
xquery-3.0
atl
bytecode-manipulation
wixsharp
simplexml
gesture
libvpx
starteam
opshub
trim
squib
galen
vao
wptoolkit
qwt
logfiles
hibernate-tools
promela
idisposable
colorama
knockout-components
knpmenubundle
word-vba-mac
nsfilemanager
qcustomplot
essence
browser-link
r-tree
metaclass
qdialog
content-length
ionic
twirl
event-bubbling
generic-programming
device-orientation
sorl-thumbnail
java.util.concurrent
socketexception
contenttype
resty-gwt
ember-charts
ms-project-server-2010
gridfs
dataservice
ora-00911
buster.js
back-stack
android-screen-support
jquery-knob
trusted
coderush
newtonscript
nsmanagedobject
fireworks
qt-jambi
mod-auth
asp.net-profiles
database-management
iweb
mtj
xetex
django-notification

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App