Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Got another one
#1
This one seems to have the entire class stumped. I'll be damned if I can't figure out how to do it. I'll post the homework question, and anyone who might have an idea on how to get started can reply. Stuff is darned difficult.

"(10 points) Write a program that takes as input two opposite corners of a
rectangle: (x1,y1) and (x2,y2) – float or integer only. Finally, the user is prompted
for the coordinates of a third point (x,y). The program should print Boolean value
True or False based on whether the point (x,y) lies within the rectangle. At the
end of each run, the user should be prompted to ask whether then want to
continue.
Note that the rectangle could fall anywhere in the 2X2 plane of real numbers."
Reply
#2
You just need to check:
if x3 >= x1 and x3 <= x2:  #<---- means it is in range from left to right

and 

if  y3 >= y1 and y3 <= y2: #<---- means it is in range from up and down
If both those are True then it's in the rectangle.
Reply
#3
Two points does not determine a rectangle, so take the simple case. One line will go from x1,y1 to x2, y1, next x1,y1 to x1, y2, etc.

A point will be within the rectangle if the X value is between x1 and x2 AND the Y value is between y1 and y2.
Done.
Reply
#4
(Feb-17-2020, 01:17 AM)michael1789 Wrote: You just need to check:
if x3 >= x1 and x3 <= x2:  #<---- means it is in range from left to right

and 

if  y3 >= y1 and y3 <= y2: #<---- means it is in range from up and down
If both those are True then it's in the rectangle.

Ah! Thank you. I'm assuming that I can use different input statements and assignment operators for the variables, and then use this statement? Also, you'll have to forgive me, but how will I structure it to show that it is True?
Reply
#5
You can make the check all one line. That's as simple as I can figure out.
if x3 >= x1 and x3 <= x2 and y3 >= y1 and y3 <= y2:
    point_is_in_rectangle = True
else:
    point_is_in_rectangle = False 
Reply
#6
You don't even need the if and else. Think about it:

x3 >= x1 and x3 <= x2 and y3 >= y1 and y3 <= y2
is an expression that evaluates to True or False. Based on which, you're setting the value of point_is_in_rectangle - if it's True, you set the variable to True and similarly if it evaluates to False.

So, one can just write

point_is_in_rectangle = x3 >= x1 and x3 <= x2 and y3 >= y1 and y3 <= y2
Reply
#7
(Feb-17-2020, 06:35 AM)ndc85430 Wrote: You don't even need the if and else. Think about it:

x3 >= x1 and x3 <= x2 and y3 >= y1 and y3 <= y2
is an expression that evaluates to True or False. Based on which, you're setting the value of point_is_in_rectangle - if it's True, you set the variable to True and similarly if it evaluates to False.

So, one can just write

point_is_in_rectangle = x3 >= x1 and x3 <= x2 and y3 >= y1 and y3 <= y2

Yeah, man, that's what I ended up doing. I can't tell you how thankful I am for the help and the explanations. I think I finally figured it out yesterday (I was so busy messing around with it I forgot to keep checking this board). Also, the line about the Boolean statements isn't entirely accurate, I suppose, but still. Here's the final result:

x1 = int(input('Enter the first horizontal value: '))
y1 = int(input('Enter the first vertical value: '))
x2 = int(input('Enter the second horizontal value: '))
y2 = int(input('Enter the second vertial value '))
x = int(input('Please enter an integer '))
y = int(input('Please enter another integer ')) #At this point we've entered all of our points, including the ones we want to tes
if x >= x1 and x <= x2 and y >= y1 and y <= y2: # Using Boolean statements to ensure that our point is within the rectangle.
    print(True)
else:
    print(False)

while True:
    line=input('Do you want to continue (Y/N)? ')
    if line =='Yes':
        continue
    if line =='No':
        break #Asks us to continue or stop, depending on the answer we use.
Reply
#8
Note that you do not have True and False in quotes in lines 8 and 10. So, you could just
print(x >= x1 and x <= x2 and y >= y1 and y <= y2) # Using Boolean statements to ensure that our point is within the rectangle.
and get rid of lines 8, 9, and 10
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020