Back to index


Rect Array

Class Methods

 collide_lists  union_list  x  x=  y  y=  w  w=  h  h=

Instance methods

 left  left=  right  right=  top  top=  bottom  bottom=  normalize  normalize!  move  move!  inflate  inflate!  union  union!  contains?  same_size?  copy_from  set_coordinates  find_overlapping_rect  find_overlapping_rects  clip  clip!  clamp  clamp!  overlaps?  cross_lines


Rect has been discarded. Its methods have moved to the standard Array. All these methods are now written in C.


The standard Ruby array class has been extended to provide methods for using it as a rectangle. Arrays like these have entries 0, 1, 2 and 3 set to floating point values for x, y, width and height.

[10,15,20,25] would be a 20x25 rectangle at position 10,15.

So far these methods are mostly untested.

Class Methods

Array.collide_lists( l1, l2 )

This method looks through list l1, checking collisions with every object in list l2. It does this by calling "rect" on all objects, expecting an array of [x,y,w,h] back, defining the area this object is in. It yields (object_from_l1, object_from_l2) for every collision it detects. A more advanced collision detection method can be found in CollisionMap.

Array.union_list( array_of_rects )

Returns a new array representing a rectangle covering all rectangles in array_of_rects.

Array#x=( x )
Array#y=( y )
Array#w=( w )
Array#h=( h )

These can be set and read at will. w and h have width and height as aliases.

Instance methods

Array#left=( left )
Array#right=( right )
Array#top=( top )
Array#bottom=( bottom )

These can be set and read at will. Differences with x, y, w and h are:


If w and h aren't positive, this will change them to positive and keep the rectangle covering the same space.

Array#move( delta )
Array#move!( delta )

Returns a new rectangle which is the base rectangle moved by the given amount.

Array#inflate( sizes )
Array#inflate!( sizes )

Returns a rectangle which has the sizes changed by the given amounts. sizes is an array of [dx, dy]. The rectangle shrinks and expands around the rectangle's center. Negative values will shrink the rectangle.

Array#union( rect )
Array#union!( rect )

Returns a new rectangle that completely covers the given rectangle(s). There may be an area inside the new rectangle that is not covered by the inputs. Rectangles are pre-normalized.

Array#contains?( thing )

Returns whether thing ([x, y, w, h] or [x, y]) fits completely within the rectangle.

Array#same_size?( rect )

Returns whether rect is the same area as self.

Array#copy_from( rect )

Sets self to the same position and size as rect. This is meant for optimizations. Returns self.

Array#set_coordinates( x, y, w, h )

Sets all properties at once. This is meant for optimizations. Returns self.

Array#find_overlapping_rect( rects )

Returns the first rectangle in the rects array to overlap the base rectangle. Once an overlap is found, this will stop checking the remaining array. If no overlap is found, it will return false.

Array#find_overlapping_rects( rects )

Returns an array with the rectangles in the list to overlaps the base rectangle. If no overlaps is found, it will return [].

Array#clip( rect )
Array#clip!( rect )

Returns a new rectangle that is the given rectangle cropped to the inside of the base rectangle. If the two rectangles do not overlaps to begin with, you will get a rectangle with 0 size.

Array#clamp( rect )
Array#clamp!( rect )

Returns a new rectangle that is moved to be completely inside the base rectangle. If the given rectangle is too large for the base rectangle in an axis, it will be centered on that axis.

Array#overlaps?( rect )

Returns true if any area of the two rectangles overlapss.

Pit.cross_lines( a, b, c, d )

Returns the coordinate where lines a to b and c to d cross, an array of [x,y,x2,y2] if they are parallel and overlapping, an array of [x,y] if they are parallel and touch at only one point, or false if they don't cross. a, b, c and d are coordinates as always: [x,y]

The results may have small precision errors, so don't use it to compare directly with other numbers.

Back to index