Write a program in Python language that receives as input a pair of coordinates (x, y) and prints on the screen "OUT" if the pair is outside the drawn rhombus (see Figure), on the contrary you print "IN" if the couple is inside. Handle cases where the coordinate pair falls on the perimeter of the square, by printing “PERIMETER”.
Tip: search on the web for the equation of the line passing through two points and use them to solve the problem
#🔒 I need help with my assignments 😭
57 messages · Page 1 of 1 (latest)
@lofty dove
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
oh this is so difficult
isnt that "rhombus" like
just a 45 degree rotated square
shouldnt |x| + |y| <= 2 be enough
Ah yes. These are four very complex quadratic functions.
they are not quadratic tho
the go in a straight line so they are linear
!e
def in_or_out(x, y):
a = abs(x) + abs(y)
if a < 2:
return 'IN'
elif a == 2:
return 'PERIMETER'
return 'OUT'
print(in_or_out(3, 4))
print(in_or_out(1, 1))
print(in_or_out(1, .5))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | OUT
002 | PERIMETER
003 | IN
what is abs
!d abs
absolute value
like |x| in math
abs(x)```
Return the absolute value of a number. The argument may be an integer, a floating\-point number, or an object implementing [`__abs__()`](https://docs.python.org/3/reference/datamodel.html#object.__abs__). If the argument is a complex number, its magnitude is returned.
our teacher wants us to use the y = -+x +-2 (depending on the quadrant)
|x| + |y| <= 2 is incorrect
it isnt
Okay, so people went straight to smart solutions instead of explaining why, despite our rules saying one shouldn't give out direct solutions to homework...
So here's an explanation:
You know how in maths a function tells you when a point belongs to that function? If you put x in the function and calculate the result it will be equal to y.
If your y is lower than calculated f(x), it means your point is below the function. If it's higher, your point is above the function.
Here you can divide the area depending on whether x is positive or negative and same for y - 4 areas, 4 formulas. The areas are already marked with numbers in your picture.
(+ When x or y is 0, then you get an easy if check)
(+ When x or y is below -2 or above 2? Won't fall inside or on the edge, it's automatic out.)
For other (x,y), you grab a function for the area it's in, calculate f(x) and see how it compares to y.
Given this^, you should be now able to code the solution. There will be a lot of ifs.
The ifs can be compressed into the smart check people gave you with abs.
alright thanks everyone
wow genius
so the Y always has to be lower in every quadrant so that the point is inside the figure
like for every equation
no my bad it doesn’t
for the 3rd and 4th one the Y has to be bigger right?
oh my god what do you mean
sorry thanks
Yep.
so in the first and second quadrant a if a point is inside the figure the Y has to be smaller than the equation (y<-x+2 and y< x+2)
and in the third and forth the Y has to be bigger than the equation in order to be inside the figure right?
so y> -x-2 and y> x-2
yes it works
i just need to make sure that if x== 0 or Y== 0 it gives me a result
yes yes
-2<x<0 and -x+2<y<x+2
or 0<x<2 and x-2<y<-x+2
idk
I'm not good at math
haha
x = float(input("Inserisci il valore di x: "))
y = float(input("Inserisci il valore di y: "))
#se il punto e nel primo quadrante
if (y > 0 and x > 0):
if y == -x+2:
print("Il punto è sul perimetro")
elif y < - x+2:
print("Il punto è all'interno")
else:
print("il punto e fuori dalla figura")
#se il punto e nel secondo quadrante
if (y > 0 and x < 0):
if y == x+2:
print("Il punto è sul perimetro")
elif y < x+2:
print("Il punto è all'interno")
else:
print("il punto e fuori dalla figura")
#se il punto e nel terzo quadrante
if (y < 0 and x < 0):
if y == -x-2:
print("Il punto è sul perimetro")
elif y > -x-2:
print("Il punto è all'interno")
else:
print("il punto e fuori dalla figura")
#se il punto e nel quarto quadrante
if (y < 0 and x > 0):
if y == x-2:
print("Il punto è sul perimetro")
elif y > x-2:
print("Il punto è all'interno")
else:
print("il punto è fuori dalla figura")
#se una delle due coordinate appartiene agli assi
if y == 0:
if x == abs(2):
print("Il punto e sul perimetro")
else:
print("Il punto è all'interno")
elif x == 0:
if y == abs(2):
print("Il punto è sul perimetro")
else:
print("Il punto è all'interno")
can someone check if the spaces are alright
wait nvm
i need to fix the if statements
its correct tho
beautiful
yeah you guys are right i could’ve written a way shorter code but i don’t care
most people in my class didn’t manage to do it
both approaches are valid and teach different things
your approach is what the prof asked for
yeah
thank you guys i appreciate everything
is there a way for me to send a python file from phone?
i did this program on my phone which was a pain in the ass
As in to discord? We don't allow code files in here because discord doesn't show preview for them (bot will automatically delete it). But there is https://paste.pythondiscord.com to share longer code
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.