Class Rubygame::Ftor
In: lib/rubygame/ftor.rb
lib/rubygame/ftor.rb
Parent: Object

NOTE: Ftor is DEPRECATED and will be removed in Rubygame 3.0! A mostly-compatible vector class will be provided at or before that time.

NOTE: you must require ‘rubygame/ftor’ manually to gain access to Rubygame::Ftor. It is not imported with Rubygame by default!

Ftor ("Fake vecTOR"), a vector-like class for 2D position/movement.

(NB: See angle for an important note about why angles appear to be the opposite of what you may expect.)

Ftor is useful for storing 2D coordinates (x,y) as well as vector quantities such as velocity and acceleration (representationally, points and vectors are equivalent.) Although Ftors are always represented internally as Cartesian coordinates (x, y), it is possible to deal with an Ftor as polar coordinates (angle, magnitude) instead. See new_am and set_am!, for example.

Ftor is a "fake" vector because it has certain convenient properties which differ from "true" vectors (i.e. vectors in a strict mathematical sense).

Unlike vectors, Ftors may be multiplied or divided to another Ftor. This is equivalent to multiplying or dividing each component by the corresponding component in the second Ftor. If you like, you can think of this feature as scaling each component of the Ftor by a separate factor:

  Ftor(a,b) * Ftor(c,d)  =  Ftor(a*c, b*d)

Of course, Ftors also have the usual vector behavior for addition/subraction between two Ftors, and multiplication/division of an Ftor by a scalar:

  Ftor(a,b) + Ftor(c,d) = Ftor(a+c, b+d)
  Ftor(a,b) * n = Ftor(a*n, b*n)

Additionally, Ftor contains functions for manipulating itself. You can both get and set such properties as angle, magnitude, unit, and normal, and the Ftor will change in-place as needed. For example, if you set angle=, the vector will change to have the new angle, but keeps the same magnitude as before.

Ftor attempts to save processing time (at the expense of memory) by storing secondary properties (angle, magnitude, etc.) whenever they are calculated,so that they need not be calculated repeatedly. If the vector changes, the properties will be calculated again the next time they are needed. (In future versions, it may be possible to disable this feature for certain Ftors, for example if they will change very often, to save memory.)

Methods

*   *   +   +   -   -   -@   -@   /   /   ==   ==   []   []   _clear   _clear   a   a   a=   a=   align!   align!   angle   angle   angle=   angle=   angle_with   angle_with   dot   dot   inspect   inspect   inspect_am   inspect_am   m   m   m=   m=   magnitude   magnitude   magnitude=   magnitude=   n   n   n=   n=   new   new   new_am   new_am   new_from_to   new_from_to   normal   normal   normal=   normal=   pretty   pretty   pretty_am   pretty_am   reverse!   reverse!   rotate   rotate   rotate!   rotate!   set!   set!   set_am!   set_am!   to_a   to_a   to_ary   to_ary   to_s   to_s   to_s_am   to_s_am   u   u   u=   u=   udot   udot   unit   unit   unit=   unit=   x=   x=   y=   y=  

Constants

PI = Math::PI
HALF_PI = PI*0.5
THREE_HALF_PI = PI*1.5
TWO_PI = PI*2
PI = Math::PI
HALF_PI = PI*0.5
THREE_HALF_PI = PI*1.5
TWO_PI = PI*2

Attributes

x  [R] 
x  [R] 
y  [R] 
y  [R] 

Public Class methods

Create a new Ftor by specifying its x and y components. See also new_am and new_from_to.

Create a new Ftor by specifying its x and y components. See also new_am and new_from_to.

Create a new Ftor by specifying its angle (in radians) and magnitude. See also new.

Create a new Ftor by specifying its angle (in radians) and magnitude. See also new.

Returns a new Ftor which represents the difference in position of two points +p1+ and +p2+. (+p1+ and +p2+ can be Ftors, size-2 Arrays, or anything else which has two numerical components and responds to #[].)

In other words, assuming v is the Ftor returned by this function:

  p1 + v = p2

Returns a new Ftor which represents the difference in position of two points +p1+ and +p2+. (+p1+ and +p2+ can be Ftors, size-2 Arrays, or anything else which has two numerical components and responds to #[].)

In other words, assuming v is the Ftor returned by this function:

  p1 + v = p2

Public Instance methods

Perform multiplication of this Ftor by the scalar other, like so:

  Ftor(a,b) * n = Ftor(a*n, b*n)

However, if this causes TypeError, attempt to extract indices 0 and 1 with other‘s #[] operator, and multiply them into the corresponding components of this Ftor, like so:

  Ftor(a,b) * Ftor(c,d) = Ftor(a*c, b*d)
  Ftor(a,b) * [c,d]     = Ftor(a*c, b*d)

Perform multiplication of this Ftor by the scalar other, like so:

  Ftor(a,b) * n = Ftor(a*n, b*n)

However, if this causes TypeError, attempt to extract indices 0 and 1 with other‘s #[] operator, and multiply them into the corresponding components of this Ftor, like so:

  Ftor(a,b) * Ftor(c,d) = Ftor(a*c, b*d)
  Ftor(a,b) * [c,d]     = Ftor(a*c, b*d)

Perform vector addition with this Ftor and other, adding them on a component-by-component basis, like so:

  Ftor(a,b) + Ftor(c,d)  =  Ftor(a+c, b+d)

Perform vector addition with this Ftor and other, adding them on a component-by-component basis, like so:

  Ftor(a,b) + Ftor(c,d)  =  Ftor(a+c, b+d)

Like #+, but performs subtraction instead of addition.

Like #+, but performs subtraction instead of addition.

The reverse of this Ftor. I.e., all components are negated. See also reverse!.

The reverse of this Ftor. I.e., all components are negated. See also reverse!.

Like #*, but performs division instead of multiplication.

Like #*, but performs division instead of multiplication.

True if this Ftor is equal to other, when both have been converted to Arrays via to_a. In other words, a component-by-component equality check.

True if this Ftor is equal to other, when both have been converted to Arrays via to_a. In other words, a component-by-component equality check.

Return the +i+th component of this Ftor, as if it were the Array returned by to_a.

Return the +i+th component of this Ftor, as if it were the Array returned by to_a.

Clears stored values for angle, magnitude, normal, and unit, so that they will be recalculated the next time they are needed. Intended for internal use, but might be useful in other situations.

Clears stored values for angle, magnitude, normal, and unit, so that they will be recalculated the next time they are needed. Intended for internal use, but might be useful in other situations.

a()

Alias for angle

a()

Alias for angle

a=(a)

Alias for angle=

a=(a)

Alias for angle=

align!(other)

Alias for unit=

align!(other)

Alias for unit=

Return the angle (radians) this Ftor forms with the positive X axis. This is the same as the Ftor‘s angle in a polar coordinate system.

IMPORTANT: Because the positive Y axis on the Rubygame::Screen points downwards, an angle in the range 0..PI will appear to point downwards, rather than upwards! This also means that positive rotation will appear clockwise, and negative rotation will appear counterclockwise! This is the opposite of what you would expect in geometry class!

Return the angle (radians) this Ftor forms with the positive X axis. This is the same as the Ftor‘s angle in a polar coordinate system.

IMPORTANT: Because the positive Y axis on the Rubygame::Screen points downwards, an angle in the range 0..PI will appear to point downwards, rather than upwards! This also means that positive rotation will appear clockwise, and negative rotation will appear counterclockwise! This is the opposite of what you would expect in geometry class!

Set the angle (radians) of this Ftor from the positive X axis. Magnitude is preserved.

Set the angle (radians) of this Ftor from the positive X axis. Magnitude is preserved.

Return the difference in angles (radians) between this Ftor and other.

Return the difference in angles (radians) between this Ftor and other.

Return the dot product (aka inner product) of this Ftor and other. The dot product of two vectors +v1+ and +v2+ is:

  v1.x * v2.x + v1.y * v2.y

Return the dot product (aka inner product) of this Ftor and other. The dot product of two vectors +v1+ and +v2+ is:

  v1.x * v2.x + v1.y * v2.y

Same as to_s, but this Ftor‘s object_id is also displayed.

Same as to_s, but this Ftor‘s object_id is also displayed.

Same as to_s_am, but this Ftor‘s object_id is also displayed.

Same as to_s_am, but this Ftor‘s object_id is also displayed.

m()

Alias for magnitude

m()

Alias for magnitude

m=(m)

Alias for magnitude=

m=(m)

Alias for magnitude=

Returns the magnitude of the Ftor, i.e. its length from tail to head. This is the same as the Ftor‘s magnitude in a polar coordinate system.

Returns the magnitude of the Ftor, i.e. its length from tail to head. This is the same as the Ftor‘s magnitude in a polar coordinate system.

Modifies the magnitude of the Ftor, preserving its angle.

In other words, the Ftor will point in the same direction, but it will be a different length from tail to head.

Modifies the magnitude of the Ftor, preserving its angle.

In other words, the Ftor will point in the same direction, but it will be a different length from tail to head.

n()

Alias for normal

n()

Alias for normal

n=(other)

Alias for normal=

n=(other)

Alias for normal=

Return a new unit Ftor which is perpendicular to this Ftor (rotated by pi/2 radians, to be specific).

Return a new unit Ftor which is perpendicular to this Ftor (rotated by pi/2 radians, to be specific).

Rotate this Ftor in-place, so that it is perpendicular to other. This Ftor will be at an angle of -pi/2 to other.

Rotate this Ftor in-place, so that it is perpendicular to other. This Ftor will be at an angle of -pi/2 to other.

"Pretty print". Same as to_s, but components are displayed as rounded floats to 3 decimal places, for easy viewing.

"Pretty print". Same as to_s, but components are displayed as rounded floats to 3 decimal places, for easy viewing.

"Pretty print" with angle and magnitude. Same as to_s_am, but components are displayed as rounded floats to 3 decimal places, for easy viewing.

"Pretty print" with angle and magnitude. Same as to_s_am, but components are displayed as rounded floats to 3 decimal places, for easy viewing.

Like #-@, but reverses this Ftor in-place.

Like #-@, but reverses this Ftor in-place.

Like rotate!, but returns a duplicate instead of rotating this Ftor in-place.

Like rotate!, but returns a duplicate instead of rotating this Ftor in-place.

Rotate this Ftor in-place by angle (radians). This is the same as adding angle to this Ftor‘s angle.

IMPORTANT: Positive rotation will appear clockwise, and negative rotation will appear counterclockwise! See angle for the reason.

Rotate this Ftor in-place by angle (radians). This is the same as adding angle to this Ftor‘s angle.

IMPORTANT: Positive rotation will appear clockwise, and negative rotation will appear counterclockwise! See angle for the reason.

Modify the x and y components of the Ftor in-place

Modify the x and y components of the Ftor in-place

Modify the angle (in radians) and magnitude of the Ftor in-place

Modify the angle (in radians) and magnitude of the Ftor in-place

Returns an Array of this Ftor‘s components, [x,y].

Returns an Array of this Ftor‘s components, [x,y].

to_ary()

Alias for to_a

to_ary()

Alias for to_a

Display this Ftor in the format: "#<Ftor: [x, y]>". x and y are displayed as floats at full precision. See also pp.

Display this Ftor in the format: "#<Ftor: [x, y]>". x and y are displayed as floats at full precision. See also pp.

Display this Ftor in the format: "#<Ftor:AM: [angle, magnitude]>". angle and magnitude are displayed as floats at full precision. See also to_s and pp_am.

Display this Ftor in the format: "#<Ftor:AM: [angle, magnitude]>". angle and magnitude are displayed as floats at full precision. See also to_s and pp_am.

u()

Alias for unit

u()

Alias for unit

u=(other)

Alias for unit=

u=(other)

Alias for unit=

Return the dot product of unit vectors of this Ftor and other.

Return the dot product of unit vectors of this Ftor and other.

Return the unit vector of the Ftor, i.e. an Ftor with the same direction, but a magnitude of 1. (This is sometimes called a "normalized" vector, not to be confused with a vector‘s normal.)

Return the unit vector of the Ftor, i.e. an Ftor with the same direction, but a magnitude of 1. (This is sometimes called a "normalized" vector, not to be confused with a vector‘s normal.)

Rotates this Ftor in-place, so that its unit vector matches the unit vector of the given Ftor.

In other words, changes the angle of this Ftor to be the same as the angle of the given Ftor, but this Ftor‘s magnitude does not change.

Rotates this Ftor in-place, so that its unit vector matches the unit vector of the given Ftor.

In other words, changes the angle of this Ftor to be the same as the angle of the given Ftor, but this Ftor‘s magnitude does not change.

Set the x component of the Ftor.

Set the x component of the Ftor.

Set the y component of the Ftor.

Set the y component of the Ftor.

[Validate]