Skip to content

Instantly share code, notes, and snippets.

@matutter
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save matutter/5f737326773ada273bd1 to your computer and use it in GitHub Desktop.

Select an option

Save matutter/5f737326773ada273bd1 to your computer and use it in GitHub Desktop.
An example class of cartisian graphs and rational numbers. although rational numbers is really just a class x > delegate(float) with a hat.
# mat utter 10/25/2014
# ...arrays of fractions - find the sum of an array.. is this an integral number class or not? Your directions are very unclear.
class Rational2
def initialize( n = 0 )
@num = n.to_f
@roundTo = 100
end
def +(other)
return Rational2.new( @num + other )
end
def -(other)
return Rational2.new( @num - other )
end
def *(other)
return Rational2.new( @num * other )
end
def /(other)
throw "Divide by zero" if other == 0
return Rational2.new( @num / other )
end
def new( n )
@num = n.to_f
end
def round_to( n )
return false if n%10 != 0
@roundTo = n
end
def to_i
return @num.to_i
end
def to_f
return @num
end
def to_s
n = @num
i = n.to_i
s = fracString( n-i )
return " #{i} #{s} "
end
def fracString( n )
i = @roundTo
n = n * @roundTo
n = n.to_i
x = n.gcd( i )
#print "GCD == #{x} of #{n} & #{i} \n\n"
i = i / x
n = n / x
return "#{n.to_i}/#{i.to_i}"
end
end
a = Rational2.new( 3.28897654 )
print "x--> #{ a } \n"
a.round_to(10)
print "x--> #{ a } \n"
a.round_to(10000)
a = a + 3
a = a - 4
a = a * 3.1
a = a / 4
print "x--> #{ a } \n"
# mat utter 10/24/2014
# accessors done
# mutators done
# to string done
# clone done
# equal done
# PIN done
# reflect x,y done
# mirror x,y done
# distance to the origin, done
# Manhattan distance, done
# dot product, done
# distances types between any two points, done
# reverse of a point done, this isn't a math term, but you can call reverse on the entire graph
# angle between the vectors represented by two NON-ZERO vector ? unsure what you're referring to
class Point
def initialize
@G = {}
@mag = 0 #magnitude
@reflect_state = []
@mirror_state = []
@position = [ :x, :y ]
@elems = 0
self
end
######################################################################
# mutators
def add( x, y )
_add( @G, x, y )
self
end
def add_series( array )
return raise "Uneven ordinates #{array.size}" if array.size.odd?
for i in 0..array.size-1
next if i.odd?
_add( @G, array[i],array[i+1] )
end
self
end
def Reflect!( list, deep = true )
@reflect_state = list if ValidatesPosition(list)
_Reflect if deep
self
end
def Mirror!( list, deep = true )
@mirror_state = list if ValidatesPosition(list)
_Mirror if deep
self
end
#####################################################################
# other
def ValidatesPosition( list )
list.each do | p |
return raise "Axis unknown #{p}" if [email protected]?(p)
end
return true
end
def PINat( x, y )
return x<0 ? x*7 : x*3 + y<0 ? y*5 : y*11 if @G.include?([x,y])
return false
end
def OriginDistance( x,y )
return Math.sqrt( x**2 + y**2 )
end
def ManhattanDistance( x1, y1, x2, y2 )
return ( x1-x2 ).abs + ( y1-y2 ).abs
end
def TwoPointsDiststance( x1, y1, x2, y2 )
return Math.sqrt( (x2-x1)**2 + (y2-y1)**2 )
end
def Reverse!
g = {}
@G.each_key do | p |
y,x = p
_add(g, x, y)
end
@G = g
self
end
# assume array of coordinate pairs
def Product( array )
return false if !array.size
t = 0
array.each do | p |
return false if p.size != 2
t += p[0] * p[1]
end
return t
end
#############################################################
# to string
def to_s
x1, x2, incrX, y1, y2, incrY = orientation
_to_s( @G, x1, x2, incrX, y1, y2, incrY )
end
#############################################################
# clone
def clone
_dup(self)
end
##############################################################
# equal & accessors
alias_method :==, :equal?
def equal?(other)
@G == other.get_grid
end
#attr_writer :G, :Gx, :Gy
def get_grid
@G
end
private
def _Mirror
return if @mirror_state.size == 0
if @mirror_state.include?( :x )
g = _dup(@G)
g.each_key do | p |
x,y = p
_add( @G, -x, y )
end
end
if @mirror_state.include?( :y )
g = _dup(@G)
g.each_key do | p |
x,y = p
_add( @G, x, -y )
end
end
end
def _Reflect
if @reflect_state.include?( :y )
g = {}
@G.each_key do | p |
x,y = p
_add( g, -x, y )
end
@G = _dup(g)
end
if @reflect_state.include?( :x )
g = {}
@G.each_key do | p |
x,y = p
_add( g, x, -y )
end
@G = _dup(g)
end
end
# private add, post sanitize
def _add( g, x, y )
key = [x,y]
if g.has_key?( key )
g[ key ] += 1
else
g[ key ] = 1
end
#@Gx[ x ] = y
#@Gy[ y ] = x
@mag = x if x > @mag
@mag = y if y > @mag
@elems += 1
end
# to string
def _to_s( g, x1, x2, incrX, y1, y2, incrY )
ord = 0
s = ""
y = y1
until y == y2 do
x = x1
until x == x2 do
val = g[ [x,y] ]
s += val ? "*" : x == 0 ? "|" : y==0 ? "-" : " "
x += incrX
end
y += incrY
s+= "\n"
end
s
end
def _dup(a)
return Marshal.load( Marshal.dump(a) )
end
def orientation
fromX = -@mag
toX = @mag
fromY = @mag
toY = -@mag
incrX = 1
incrY = -1
return fromX, toX, incrX, fromY, toY, incrY
end
def pad( s, size )
s.to_s.rjust( size, ' ' )
end
end
p = Point.new
p.add(11,11)
p.add_series( [9,9,10,10,9,8,4,5,6,7,4,2,5,2,2,4,2,5,3,2,3,3,3,4,0,0,1,1,4,3,8,8,8,7,1,2,3,4,5,6,7,8,9,10,2,2,4,4,7,8,4,6,0,8,0,7,0,1,0,2,0,4,0,5] )
print "Distance 9,8 & 4,5 = ", p.TwoPointsDiststance(9,8,4,5), "\n"
print "Product 1,2 2,3 3,4 4,5 = ", p.Product( [[1,2],[2,3],[3,4],[4,5]] ), "\n"
print "Manhattan Dist between (9,9) and (5,2) = ", p.ManhattanDistance(9,9,5,2), "\n"
print "PIN hash, (5,2) = ", p.PINat(5,2), "\n"
print "Distance to origin, (5,2) = ", p.OriginDistance(5,2), "\n"
print "#{p.clone.Reflect!([:x]).Mirror!([:x,:y])}\n"
print "#{p}\n"
print "#{p.Reflect!([:y])}\n"
print "#{p.Reverse!} \n"
print "#{p.Reflect!([:y])}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment