Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  finding a line length  (Read 1463 times)
0 Members and 1 Guest are viewing this topic.
Offline westloar

Junior Devvie





« Posted 2012-04-27 21:13:29 »

What's the best way of find a line's length, subtracting one Point from another seems clunky and I can't see an inbuilt method in Line2D, is there a better way?
Offline ra4king

JGO Kernel


Medals: 355
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-04-27 21:20:28 »

What about the elementary line distance formula which you should have learned in middle school/early high school? (unless you're younger Shocked)

sqrt((x1-x2) ^ 2 + (y1-y2) ^ 2)

1  
double distance = Math.sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2));


Smiley

Offline krasse
« Reply #2 - Posted 2012-04-27 21:36:59 »

Also, the following static methods exist:

1  
2  
Point2D.distance(double x1, double y1, double x2, double y2)
Point2D.distanceSq(double x1, double y1, double x2, double y2)



Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StumpyStrust
« Reply #3 - Posted 2012-04-27 21:54:18 »

...

Offline sproingie

JGO Kernel


Medals: 202



« Reply #4 - Posted 2012-04-27 21:56:42 »

Yes, not taking the square root if you don't have to is a nice optimization, but the question was about finding distance.  Let's not confuse people unnecessarily...
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 835
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2012-04-27 22:05:54 »

I have to agree with sproingie: I think optimizing out Math.sqrt(...) and replacing it with two Math.pow(...)'s is offtopic, confusing and hilarious.  Not to mention the if/else blocks that return false and true Wink

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline westloar

Junior Devvie





« Reply #6 - Posted 2012-04-27 22:36:19 »

thanks, I'll use the solution from Point2D methinks, much easier to use and get my tiny brain around Cheesy
Offline StumpyStrust
« Reply #7 - Posted 2012-04-28 00:45:21 »

Ehh I know it may have been a little too off topic but I think he would have been able to figure it out in like 2 secs from the formula and the 2 Math.pows was because I was lazy and hate typing code into a post.

In my actual code I try to use as little possible from the Math class. And sorry if it was too confusing...I am not the best at explaining things.

Offline ReBirth
« Reply #8 - Posted 2012-04-28 03:15:23 »

If you want to avoid square root in collision, try to check both X and Y first without bring them to pythagoras.

Offline Roquen
« Reply #9 - Posted 2012-04-28 17:54:18 »

Because typing: Math.pow(x,2) is shorter than x*x?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 835
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2012-04-28 17:57:43 »

To be fair, in his example that would be:
   Math.pow((x1-x2),2)
   (x1-x2)*(x1-x2)
which still proves your point Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline StumpyStrust
« Reply #11 - Posted 2012-04-28 22:59:16 »

Geez normally I don't take offense to stuff but man it really seems like I must have come across rude or something because you guys are down my throat here. I will admit that I was wrong in my earlier post that Math.pow or what ever is longer but come on now no need to point out the obvious error.

If it means that much to you guys I will change/remove my earlier posts so no one else will have to suffer from my horrible coding examples/posts. Just trying to help.  Clueless


Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 835
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2012-04-28 23:08:38 »

Don't take it so personal.

There was a question, you answered it with something that actually did not solve his problem, introduced slow code for the sake of optimization, and then you defended it by saying you wanted to provide a short code sample, while the correct and faster version was shorter.

Can't we point that out? We're not out to pick on you, just responding to what you actually said...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline StumpyStrust
« Reply #13 - Posted 2012-04-28 23:35:54 »

All true.

Offline Roquen
« Reply #14 - Posted 2012-04-29 06:25:03 »

Note that if you want the length/magnitude AND want short code then you can alway use hypot.  But you don't really want to do that in games because you shouldn't care about overflow/underflow issues.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (26 views)
2014-11-22 12:13:56

xFryIx (69 views)
2014-11-13 12:34:49

digdugdiggy (48 views)
2014-11-12 21:11:50

digdugdiggy (42 views)
2014-11-12 21:10:15

digdugdiggy (36 views)
2014-11-12 21:09:33

kovacsa (60 views)
2014-11-07 19:57:14

TehJavaDev (64 views)
2014-11-03 22:04:50

BurntPizza (62 views)
2014-11-03 18:54:52

moogie (77 views)
2014-11-03 06:22:04

CopyableCougar4 (77 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!