100000.times do |i|
Math::sin(Math::atan(i/10))
Math::cos(Math::atan(i/10))
end
def dist_form(x1, y1, x2, y2)
Math.sqrt( (x2 - x1)**2 + (y2 - y1)**2)
end
100000.times do |i|
dist_form(i, 10, 10, i)
end
require "inline"
class MyTest
inline do |builder|
builder.c "
int dist_form(int x1, int y1, int x2, int y2) {
return sqrt( (double)((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) );
}"
end
end
t = MyTest.new
100000.times do |i|
t.dist_form(i, 10, 10, i)
end
require "inline"
class MyTest
inline do |builder|
builder.c "
/* right number of calls anyways */
int trigcalls(double d1) {
return sin(atan(d1/10)) + cos(atan(d1/10));
}"
end
end
t = MyTest.new
100000.times do |i|
t.trigcalls(i)
end
Did you mean len XQ instead of XP?
Yeah. I also meant the angle QXP instead, since XP isn't an angle. ;-)
You also have to express the coordinates in absolute differences, save the signs, and add or subtract the result from your origin.
Going from 3,3 to -10,-4
QP = 13 negative direction from origin
XQ = 7 negative direction from origin
Thanks Tyche!
Got it working great.
Here's the irb to replace my first one.
=> [2.0, 5.0]
irb(main):003:0> b = [24.0, 10.0] # to
=> [24.0, 10.0]
irb(main):004:0> at = Math::atan((b[0] - a[0])/(b[1] - a[1]))
=> 1.34731972565426
irb(main):005:0> c = a[0] + Math::sin(at) * distance, a[1] + Math::cos(at) * distance
=> [6.8756642789573, 6.10810551794484]
And as Tyche said. Some stuff has to be changed for directions based on direction moving.