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

Java regexp for file filtering
How to download page for jad file at servlet?
What is the best way to connect to a DotNet web service from java?
Need a basic audio & video player library in Java [closed]
Why doesn't Java support unsigned ints?
JInternalFrame Does Not Fully Maximize Under Mac OS X
Eclipse debug-time classpath problem: How do you include a dependent project's output into a web project's runtime classpath?
How to set a timeout during remote ejb lookup?
Best Java supported server/client protocol?
Do common JARs have to be repeated accross WARs in an EAR?
Important frameworks/tools to learn
What are the lightweight options one has to persist Java objects [closed]
How do I make the method return type generic?
batch not working if calling from a Java program
Best way to list files in Java, sorted by Date Modified?
Log4j usage - how to customize the event ID when logging to Windows Event Log?

Categories

HOME
winforms
gitlab
hive
cloud
hook
netbeans
server
reserved
appx
lodash
session
analysis
amazon-ecs
sharepoint-designer
sql-server-2016
mvvmcross
ezpublish
azure-storage-tables
cross-validation
communication
ssl-client-authentication
spring-tool-suite
phaser
hapi
libtiff
openrefine
highlight.js
fatal-error
transformation
aurigma
propel
code-review
visual-composer
introduction
autocad-plugin
errorlevel
google-search-api
blazemeter
crystal-reports-2010
microsoft-sync-framework
spark-jobserver
django-storage
primitive
fabric8
phonegap
google-cloud-endpoints-v2
android-ble
libraries
starteam
lumberjack
ncalc
pdf-reactor
reportingservices-2005
websphere-mq-fte
apple-news
user-accounts
azure-application-gateway
upstart
dropbox-php
gabor-filter
sqlclient
libusb-win32
google-perftools
dtexec
hittest
gnome-shell-extensions
time-and-attendance
android-fonts
grails-tomcat-plugin
google-web-starter-kit
packagist
pagerank
design-by-contract
photobucket
freelancer.com-api
rtbkit
teamcity-8.0
thredds
kcachegrind
microbenchmark
cisco-ios
iiviewdeckcontroller
angular-local-storage
gadt
tablelayout
cctv
asp.net-web-api-odata
preferences
xojo
mesa
contenttype
valueconverter
braille
stxxl
xsockets.net
ms-project-server-2010
web2py-modules
eclipse-memory-analyzer
buster.js
html4
seed
clipper
venn-diagram
uiviewanimation-curve
netdna-api
fireworks
deobfuscation
horizontal-accordion
invite
javax.script
table-footer
netbeans-7.1
coda-slider
audio-capture
photoshop-cs4
uiq3

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App