Hello, I have been trying to get this program to work. It is supposed to
take 8 inputs, 4 x values, 4 y values, and identify the quadrilateral. I
have not been able to get it to work. Can anyone identify the problem
and offer a solution? Thanks!

Its not very long, so I pasted it here and included the .rb file.

# ========= START CODE

#Set variables

shape = “polygon”

#BEGIN USER INPUT

puts “Enter point A, X value”
xA = gets.chomp().to_i #converts input to integer

puts “Enter point A, Y value”
yA = gets.chomp().to_i #converts input to integer

puts “Enter point B, X value”
xB = gets.chomp().to_i #converts input to integer

puts “Enter point B, Y value”
yB = gets.chomp().to_i #converts input to integer

puts “Enter point C, X value”
xC = gets.chomp().to_i #converts input to integer

puts “Enter point C, Y value”
yC = gets.chomp().to_i #converts input to integer

puts “Enter point D, X value”
xD = gets.chomp().to_i #converts input to integer

puts “Enter point D, Y value”
yD = gets.chomp().to_i #converts input to integer

#END USER INPUT

#BEGIN SLOPE CALCULATIONS

slopeAB = yB-yA/xB-xA #calculates slope of side AB
slopeBC = yC-yB/xC-xB #calculates slope of side BC
slopeCD = yD-yC/xD-xC #calculates slope of side CD
slopeDA = yA-yD/xA-xD #calculates slope of side DA

#END SLOPE CALCULATIONS

#BEGINE SIDE-LENGTH CALCULATIONS

lengthAB = Math.sqrt((xB-xA)**2+(yB-yA)**2) #calculates length of
AB
lengthBC = Math.sqrt((xC-xB)**2+(yC-yB)**2) #calculates length of
BC
lengthCD = Math.sqrt((xD-xC)**2+(yD-yC)**2) #calculates length of
CD
lengthDA = Math.sqrt((xA-xD)**2+(yA-yD)**2) #calculates length of
DA

#END SIDE-LENGTH CALCULATIONS

#BEGIN MIDPOINT CALCULATIONS

midPointACx = (xA+xC)/2 #averages x of A and C
midPointACy = (yA+yC)/2 #averages y of A and C
midPointBDx = (xB+xD)/2 #averages x of B and D
midPointBDy = (yB+yD)/2 #averages y of B and D

#END MIDPOINT CALCULATIONS

#END DIAGONAL LENGTH CALCULATIONS

lengthMidAC = Math.sqrt((xC-xA)**2+(yC-yA)**2) #calculates
length of diagonal AC
lengthMidBD = Math.sqrt((xD-xB)**2+(yD-yB)**2) #calculates
length of diagonal BD

#END DIAGONAL LENGTH CALCULATIONS

#Identifies if quadrilateral has two pairs of parallel sides

if (slopeAB == slopeCD) and (slopeBC == slopeDA)
shape = “parallelogram”
else
shape = shape
end

#Identifies if quadrilateral diagonals are congruent

if (shape == “parallelogram”) and (lengthMidAC == lengthMidBD)
shape = “rectangle”
elsif (shape == “parallelogram”) and (lengthMidAC != lengthMidBD)
shape = “parallelogram”
else
shape = shape
end

#Determines if shape is a kite or not

if (lengthAB == lengthBC) and (lengthCD == lengthDA)
shape = “kite”
else
shape = shape
end

#If shape is trapezoid, identifies if shape is isosceles trapezoid

if (shape == “trapezoid”) and (lengthMidAC == lengthMidBD)
shape = “isosceles trapezoid”
else
shape = shape
end

#Identifies if shape has four congruent sides

if (lengthAB == lengthBC) and (lengthCD == lengthDA) and (lengthAB ==
lengthCD) and (lengthBC == lengthDA) and (lengthDA == lengthAB)
shape = “rhombus”
else
shape = shape
end

#But is it square?

if (shape == “rhombus”) and (slopeCD == (1-2/slopeDA)) and (slopeAB ==
(1-2/slopeCD))
shape = “square”
else
shape = shape
end

puts “=====================================”
puts “=====================================”
puts “=====================================”
puts “=====================================”
puts slopeAB
puts slopeBC
puts slopeCD
puts slopeDA

Date: ven 10 gen 14 05:53:50 +0100

Quoting Henry H. ([email protected]):

Hello, I have been trying to get this program to work. It is supposed to
take 8 inputs, 4 x values, 4 y values, and identify the quadrilateral. I
have not been able to get it to work. Can anyone identify the problem
and offer a solution? Thanks!

I won’t go into your algorithm, but it is a very bad idea to calculate
slopes in integer math. Use floating numbers: it is enough to change
the eight input lines:

xA = gets.chomp().to_i

to

xA = gets.chomp().to_f

Carlo

I haven’t looked at the whole program yet, but the slope calculations
are
missing parentheses.

slopeAB = yB-yA/xB-xA #calculates slope of side AB
slopeBC = yC-yB/xC-xB #calculates slope of side BC
slopeCD = yD-yC/xD-xC #calculates slope of side CD
slopeDA = yA-yD/xA-xD #calculates slope of side DA

should be: slopeAB = ( yB - yA ) / ( xB - xA )
#calculates
slope of side AB

Thank you all very much! I’m working on revising it now, and I will
update if I have any more problems.

First, make sure you incorporate the change suggested by Carlo, all of
the
calculations need to be done with floating point numbers or you will run
into errors.
If you run into problem with the two floats that should be equal not
returning true for == check out this stackoverflow post about comparing
floats http://tinyurl.com/mdq7caq.

Also, you should not be using ‘and’, use ‘&&’. They are not synonyms.
They
have different precedence in the order of operations. ‘and’ is intended

#Determines if shape is a kite or not

if (lengthAB == lengthBC) and (lengthCD == lengthDA)
shape = “kite”
else
shape = shape
end

# Technically, this will only identify kites oriented in one direction, you should also || a comparison for (bc == cd) && ( ab == da), to catch kites rotated 90 degrees, ie: if ( (lengthAB == lengthBC) && (lengthCD == lengthDA) ) || ( (lengthAB

lengthDA) && (lengthCD == lengthBC) )

#If shape is trapezoid, identifies if shape is isosceles trapezoid

if (shape == “trapezoid”) and (lengthMidAC == lengthMidBD)
shape = “isosceles trapezoid”
else
shape = shape
end

This will never get set since shape will never get set to trapezoid. It
looks like you forgot to put in the piece of code that checks for a
generic trapezoid.

if (shape == “rhombus”) and (slopeCD == (1-2/slopeDA)) and (slopeAB ==
(1-2/slopeCD))

You forgot some parentheses in here. You need (1-2)/slope, but that is
better written as -1/slope. So:

if (shape == “rhombus”) and (slopeCD == ( -1.0 / slopeDA)) and (slopeAB
== ( -1.0 / slopeCD))

** and notice that I used 1.0 to ensure that the calculatiions remained
floating point.

I did a quick check over the code, if you still have problems try
debugging through and make sure you are seeing the expected result of
each
calculation.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.