-2

so I currently have 2 points in the format [(x0, x1, y0, y1)] which I know are a rectangular shape but I am looking to have this list updated so that it is all 4 points of the rectangle. Note: the rectangle can only be drawn vertically or horizontally on a grid so I know that the 4 corner co-ordinates would be (x1,y1),(x1, y2),(x2, y1),(x2,y2) but I am unsure on what would be the best way to do this to have it changed to be in the desired format. I am currently trying to solve this on python

Jack Timber
  • 45
  • 1
  • 1
  • 5
  • What have you written so far? Can you show *runnable* sample code and state the desired output for the example? – André Dec 01 '21 at 12:47
  • what did you try? Where is your code? – furas Dec 01 '21 at 13:19
  • Simply assign original values to 4 variables and use these variables to create all pairs. And stop searching `the best way` - search `working way` – furas Dec 01 '21 at 13:23

1 Answers1

0

I don't know what is the problem.

Simply assign values to 4 variables and use them to create 4 pairs

Simple Example:


rect = [(0, 1, 2, 3)]
print('rect  :', rect)

x1, x2, y1, y2 = rect[0]

points = [(x1, y1),(x1, y2),(x2, y1),(x2,y2)]
print('points:', points)

Result:

rect  : [(0, 1, 2, 3)]
points: [(0, 2), (0, 3), (1, 2), (1, 3)]

And if you have many rectangles then you can use for-loop

Simple Example:

many_rects = [(0, 1, 2, 3), (10, 11, 12, 13)]
print('many_rects :', many_rects)

many_points = []

for rect in many_rects:
    x1, x2, y1, y2 = rect
    points = [(x1, y1),(x1, y2),(x2, y1),(x2,y2)]

    many_points.append(points)
    #many_points.extend(points)  # as flatten list
    
print('many_points:', many_points)

Result:

many_rects : [(0, 1, 2, 3), (10, 11, 12, 13)]
many_points: [[(0, 2), (0, 3), (1, 2), (1, 3)], [(10, 12), (10, 13), (11, 12), (11, 13)]]

# flatten list
many_points: [(0, 2), (0, 3), (1, 2), (1, 3), (10, 12), (10, 13), (11, 12), (11, 13)]
furas
  • 134,197
  • 12
  • 106
  • 148