Jun-02-2022, 07:55 PM
(This post was last modified: Jun-02-2022, 07:55 PM by deanhystad.)
Though it is not unusual to provide default arguments for a function, I think here it indicates that you don't understand how functions work.
I would expect something that looks more like this:
This is not a very good function. You cannot use it to determine if point is actually in the (rectangular?) area defined by firstCorner, secondCorner. It would be more useful if "isIn" returned True/False instead of printing True/False.
isIn is a lousy function name. In what? What is in what? Some comments might be useful too.
If you wanted to continue enhancing the code you could rewrite this as a class with attributes and methods. This code defines a Rectangle class with methods to return the rectangle area in addition to the contains method (replacement for isIn()).
I would expect something that looks more like this:
def isIn(firstCorner, secondCorner, point): x1, y1 = firstCorner x2, y2 = secondCorner test1, test2 = point if test1 >= x1 and test1 <= x2 and test2 >= y1 and test2 <= y2: print("True") elif test1 >= x1 and test1 <= x2 and test2 <= y1 and test2 >= y2: print("True") elif test1 <= x1 and test1 >= x2 and test2 >= y1 and test2 <= y2: print("True") elif test1 <= x1 and test1 >= x2 and test2 <= y1 and test2 >= y2: print("True") else: print("False") isIn(firstCorner=(1, 2), secondCorner=(3, 4), point=(1.5, 3.2))The argument values are provided by the function caller, not the function. Now you can reuse the function for any sized rectangle and any points.
This is not a very good function. You cannot use it to determine if point is actually in the (rectangular?) area defined by firstCorner, secondCorner. It would be more useful if "isIn" returned True/False instead of printing True/False.
def isIn(firstCorner, secondCorner, point): x1, y1 = firstCorner x2, y2 = secondCorner test1, test2 = point return x1 <= test1 <= x2 and y1 <= test2 <= y2 print(isIn(firstCorner=(1, 2), secondCorner=(3, 4), point=(1.5, 3.2)))
Output:True
Now that the function returns a value you can print True or False, or you can use the returned value in an if statement, or as an argument to another function. The possibilities are endless.isIn is a lousy function name. In what? What is in what? Some comments might be useful too.
def point_in_rectangle(lower_left, upper_right, point): """Return True if point is in rectangle defined by corners lowerLeft and upper right. """ llx, lly = lower_left urx, ury = upper_right x, y = point return llx <= x <= urx and lly <= y <= ury print(point_in_rectangle((1, 2), (3, 4), (1.5, 3.2)))Now the code almost reads like a book. The function name describes what the function does. The argument names describes what the arguments mean. This function got a lot easier to use.
If you wanted to continue enhancing the code you could rewrite this as a class with attributes and methods. This code defines a Rectangle class with methods to return the rectangle area in addition to the contains method (replacement for isIn()).
from dataclasses import dataclass @dataclass class Rectangle: """A 2D rectangular shape defined by the lower left corner, width and height""" x: float y: float width: float height: float def area(self): """Return area of rectangle""" return self.width * self.height def contains(self, point): """Return True if rectangle contains point""" px, py = point return ( self.x <= px <= self.x + self.width and self.y <= py <= self.y + self.height ) myrect = Rectangle(1, 2, 2, 2) for point in ((1.5, 3.2), (3, 7), (0, 3)): print( f"{myrect} Area = {myrect.area()} {'contains' if myrect.contains(point) else 'does not contain'} {point}" )
Output:Rectangle(x=1, y=2, width=2, height=2) Area = 4 contains (1.5, 3.2)
Rectangle(x=1, y=2, width=2, height=2) Area = 4 does not contain (3, 7)
Rectangle(x=1, y=2, width=2, height=2) Area = 4 does not contain (0, 3)