Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This contains a function that finds the intersecting vertices of two rectangles given the rectangles vertices as two arrays. The arrays are given such that the two first elements are the coordinate of the base point of the rectangle, while the third element is the breath and the last element is the height of the rectangle. Example a=[1,1,2,3] x=…
#The function find the intersection point of two lines
def intersect_rec(a, b)
#Function takes the coordinate of horizontal and vertical line #Return the coordinate of the intersection
intersect = []
if a.length !=4 and b.length !=4
return "You did not enter a rectangle vertices"
end
x1 = a[0]
x2 = a[0]+a[2]
y1 = a[1]
y2 = a[1]+a[3]
x3 = b[0]
x4 = b[0]+b[2]
y3 = b[1]
y4 = b[1]+b[3]
puts "x1 = #{x1}"
puts "x2 = #{x2}"
puts "x3 = #{x3}"
puts "x4 = #{x4}"
puts "y1 = #{y1}"
puts "y2 = #{y2}"
puts "y3 = #{y3}"
puts "y4 = #{y4}"
arr_of_x = [x1,x2,x3,x4]
arr_of_y = [y1,y2,y3,y4]
#The major condition that most be satisfied if the Rectangles met at all
if (y4>y1 and y3<y2) and (x4>x1 and x3<x2)
if y4>y2 and y3<y1 and ((x4>x2 and x3<x1))
intersect ="The first rectangle is inside the second rectangle"
elsif y4<y2 and y3>y1 and ((x4<x2 and x3>x1))
intersect = "The second rectangle is inside the first rectangle"
else
intersect_on_x = arr_of_x.sort
intersect_on_y = arr_of_y.sort
intersect = [[intersect_on_x[1], intersect_on_y[1]],[intersect_on_x[1], intersect_on_y[2]],[intersect_on_x[2], intersect_on_y[1]],[intersect_on_x[2], intersect_on_y[2]]]
end
else
intersect = "The rectangle do not Intersect"
end
intersect
end
b = [1,1,1,4]
a = [0,0,2,3]
puts "#{a} and #{b}"
intersect = intersect_rec(a,b)
puts "#{intersect[0]} | #{intersect[1]}"
puts "#{intersect[2]} | #{intersect[3]}"
#The function find the intersection rectangle of two rectangles
def find_intersect_rec(a, b)
#Function takes the coordinate of horizontal and vertical line
#Return the coordinate of the intersection
insect = []
if a.length !=4 and b.length !=4
return "You did not enter a rectangle vertices"
end
x1 = a[0]
x2 = a[0]+a[2]
y1 = a[1]
y2 = a[1]+a[3]
x3 = b[0]
x4 = b[0]+b[2]
y3 = b[1]
y4 = b[1]+b[3]
puts "x1 = #{x1}"
puts "x2 = #{x2}"
puts "x3 = #{x3}"
puts "x4 = #{x4}"
puts "y1 = #{y1}"
puts "y2 = #{y2}"
puts "y3 = #{y3}"
puts "y4 = #{y4}"
#The major condition that most be satisfied if the Rectangles met at all
if (y4>y1 and y3<y2) and (x4>x1 and x3<x2)
if y4>=y2
if y3>=y1 and x4<=x2 #For the
if x3<=x1 #It is the coordinate that is the greastest that get the vertice of the intersection in this case it is x1
insect = [[x1,y3],[x4,y3],[x1,y2],[x4,y2]]
elsif x3>=x1
insect =[[x3,y3],[x4,y3],[x3,y2],[x4,y2]]
end
elsif y3>=y1 and x4>=x2
if x3<=x1 #It is the coordinate that is the greastest that get the vertice of the intersection in this case it is x1
puts "We are here"
insect = [[x1,y3],[x2,y3],[x1,y2],[x2,y2]]
elsif x3>=x1
insect =[[x3,y3],[x2,y3],[x3,y2],[x2,y2]]
end
#Check for the case when the y3 is less than y1
elsif y3<=y1 and x4<=x2 #For the
if x3<=x1 #It is the coordinate that is the greastest that get the vertice of the intersection in this case it is y1
insect = [[x1,y1],[x4,y1],[x1,y2],[x4,y2]]
elsif x3>=x1
insect =[[x3,y1],[x4,y1],[x3,y2],[x4,y2]]
end
elsif y3<=y1 and x4>=x2
if x3<=x1 #It is the coordinate that is the greastest that get the vertice of the intersection in this case it is x1
insect = [[x3,y1],[x2,y1],[x3,y2],[x2,y2]]
elsif x3>=x1
return "The rectangles are not intersecting. The rectangle #{[[x3,y3],[x4,y3],[x3,y4],[x4,y4]]} the second is the Bigger Rectangle"
end
end
end
#Consider the case when the second Rectangle is not higher than the rectangle of the first
if y4<=y2
if y3>=y1 and x4<=x2 #For the
if x3<=x1 #It is the coordinate that is the greastest that get the vertice of the intersection in this case it is x1
insect = [[x1,y3],[x4,y3],[x1,y4],[x4,y4]]
elsif x3>=x1
return "The rectangles are not intersecting. The rectangle #{[[x1,y1],[x1,y2],[x2,y1],[x2,y2]]} the First is the Bigger Rectangle"
end
elsif y3>=y1 and x4>=x2
if x3<=x1 #It is the coordinate that is the greastest that get the vertice of the intersection.
insect = [[x1,y3],[x2,y3],[x1,y4],[x2,y4]]
elsif x3>=x1
insect =[[x3,y3],[x2,y3],[x3,y4],[x2,y4]]
end
#Check for the case when the y3 is less than y1
elsif y3<=y1 and x4<=x2 #For the
if x3<=x1 #It is the coordinate that is the greastest that get the vertice of the intersection in this case it is y1
insect = [[x1,y1],[x4,y1],[x1,y4],[x4,y4]]
elsif x3>=x1
insect =[[x3,y1],[x4,y1],[x3,y4],[x4,y4]]
end
elsif y3<=y1 and x4>=x2
if x3<=x1 #It is the coordinate that is the greastest that get the vertice of the intersection in this case it is x1
insect = [[x1,y1],[x2,y1],[x1,y4],[x2,y4]]
elsif x3>=x1
insect =[[x3,y1],[x2,y1],[x3,y4],[x2,y4]]
end
end
end
else
return "The Rectangles do not intersect"
end
insect
end
print [0,0,1,2],[0,0,4,3]
puts ""
puts find_intersect_rec([0,0,4,4],[1,1,1,1])
@EmmaAkin

This comment has been minimized.

Copy link
Owner Author

commented Oct 1, 2016

The first file contain a faster refactored code for the solution of the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.