Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help solving a problem
#4
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:
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)
Reply


Messages In This Thread
Need help solving a problem - by rufenghk - Jun-02-2022, 03:02 PM
RE: Need help solving a problem - by ndc85430 - Jun-02-2022, 03:35 PM
RE: Need help solving a problem - by bowlofred - Jun-02-2022, 03:37 PM
RE: Need help solving a problem - by deanhystad - Jun-02-2022, 07:55 PM
RE: Need help solving a problem - by rufenghk - Jun-02-2022, 09:26 PM
RE: Need help solving a problem - by rufenghk - Jun-02-2022, 08:58 PM
RE: Need help solving a problem - by deanhystad - Jun-03-2022, 12:31 AM
RE: Need help solving a problem - by rufenghk - Jun-04-2022, 10:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  SOlving LInear Equations in Python(Symoy, NUmpy) - Coefficient Problem quest 3 1,788 Jan-30-2022, 10:53 PM
Last Post: quest
  Problem in solving optimization problem Ruchika 0 1,605 Jul-27-2020, 05:28 AM
Last Post: Ruchika

Forum Jump:

User Panel Messages

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