# Quadrilateral Identification program

**URL:** <https://www.ruby-forum.com/t/quadrilateral-identification-program/238443>\
**Category:** Ruby\
**Created:** [January 10, 2014, 5:53am UTC](https://www.ruby-forum.com/t/quadrilateral-identification-program/238443 "2014-01-10T05:53:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [January 10, 2014, 5:53am UTC](https://www.ruby-forum.com/t/quadrilateral-identification-program/238443/1 "2014-01-10T05:53:50Z")

</div>

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

#BEGIN QUADRILATERAL IDENTIFICATION

#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 "Your quadrilateral is a " + shape  
puts “=====================================”  
puts “=====================================”  
puts slopeAB  
puts slopeBC  
puts slopeCD  
puts slopeDA

---

<div class="post-metadata">

**Author:** ![hankster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hankster](https://www.ruby-forum.com/u/hankster)\
**Post date:** [January 10, 2014, 7:23am UTC](https://www.ruby-forum.com/t/quadrilateral-identification-program/238443/2 "2014-01-10T07:23:18Z")

</div>

Subject: Quadrilateral Identification program  
Date: ven 10 gen 14 05:53:50 +0100

Quoting Henry H. ([removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)):

> 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

---

<div class="post-metadata">

**Author:** ![hankster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hankster](https://www.ruby-forum.com/u/hankster)\
**Post date:** [January 10, 2014, 12:41pm UTC](https://www.ruby-forum.com/t/quadrilateral-identification-program/238443/3 "2014-01-10T12:41:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hankster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hankster](https://www.ruby-forum.com/u/hankster)\
**Post date:** [January 10, 2014, 1:48pm UTC](https://www.ruby-forum.com/t/quadrilateral-identification-program/238443/4 "2014-01-10T13:48:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hankster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b2d939/32.png) [@hankster](https://www.ruby-forum.com/u/hankster)\
**Post date:** [January 10, 2014, 1:28pm UTC](https://www.ruby-forum.com/t/quadrilateral-identification-program/238443/5 "2014-01-10T13:28:25Z")

</div>

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](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  
for flow control. I suggest you google it and read about the difference.

> #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.
