implement draw line endpoint and method

This commit is contained in:
waltem01 2024-03-08 11:23:40 +01:00
parent 8eded89afa
commit be55d35f67

View File

@ -170,6 +170,33 @@ def draw_rectangle():
# respond to client
return jsonify(response)
@api.route('/line', methods=['POST'])
@cross_origin()
def draw_line():
# prepare response data
response = { 'success': True }
try:
global matrix
assert matrix is not None
# receive client data
data = request.form
# try unpacking x,y coordinates
x1 = int(data.get('x1'))
y1 = int(data.get('y1'))
x2 = int(data.get('x2'))
y2 = int(data.get('y2'))
# call matrix method with data
matrix.line(x1, y1, x2, y2)
except Exception as e:
# error handling
print(e)
response['success'] = False
# respond to client
return jsonify(response)
@api.route('/color', methods=['POST'])
@cross_origin()
def set_color():
@ -289,6 +316,10 @@ class Matrix(SampleBase):
# set next pixel in rectangle
self.canvas.SetPixel(x+i, y+j, self.color.red, self.color.green, self.color.blue)
# display line from (x1,y1) to (x2,y2) in current color
def line(self, x1: int, y1: int, x2: int, y2: int):
graphics.DrawLine(self.canvas, x1, y1, x2, y2, self.color)
# set current image
def set_image(self, url: str):
_, data = url.split(',')