Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- from math import *
- class drawLine:
- def __init__(self):
- self.color = 255
- command = raw_input()
- if command == "draw line":
- self.point_one_x = float(input("First point - X: "))
- self.point_one_y = float(input("First point - y: "))
- self.point_two_x = float(input("Second point - x: "))
- self.point_two_y = float(input("Second point - y: "))
- self.createCanvas()
- self.countPoints()
- elif command == "draw circle":
- self.middleX = int(input("Middle X:"))
- self.middleY = int(input("Middle Y:"))
- self.radius = int(input("Radius:"))
- self.createCanvas()
- self.createCircle()
- def createCanvas(self):
- self.image = Image.new("RGB", (255, 255), "white")
- self.pixels = self.image.load()
- def countPoints(self):
- # get equations
- y = abs(int(self.point_one_y) - int(self.point_two_y))
- x = abs(int(self.point_one_x) - int(self.point_two_x))
- if (self.point_two_x - self.point_one_x) != 0:
- a = (self.point_two_y + self.point_one_y) / (self.point_two_x + self.point_one_x)
- b = self.point_one_y - a * self.point_one_x
- print(a)
- print(b)
- if (y > x):
- if self.point_one_y > self.point_two_y:
- for mainY in range(int(self.point_two_y), int(self.point_one_y)):
- x = (mainY - b) / a
- self.pixels[int(x), int(mainY)] = (int(x), int(mainY), self.color)
- else:
- for mainY in range(int(self.point_one_y), int(self.point_two_y)):
- x = (mainY - b) / a
- self.pixels[int(x), int(mainY)] = (int(x), int(mainY), self.color)
- else:
- if self.point_one_x > self.point_two_x:
- for mainX in range(int(self.point_two_x), int(self.point_one_x)):
- y = a * mainX + b
- self.pixels[int(mainX), int(y)] = (int(mainX), int(y), self.color)
- else:
- for mainX in range(int(self.point_one_x), int(self.point_two_x)):
- y = a * mainX + b
- self.pixels[int(mainX), int(y)] = (int(mainX), int(y), self.color)
- else:
- a = self.point_two_y / self.point_two_x
- b = 0
- if self.point_one_y > self.point_two_y:
- for mainY in range(int(self.point_two_y), int(self.point_one_y)):
- self.pixels[int(self.point_two_x), int(mainY)] = (int(self.point_two_x), int(mainY), self.color)
- else:
- for mainY in range(int(self.point_one_y), int(self.point_two_y)):
- self.pixels[int(self.point_two_x), int(mainY)] = (int(self.point_two_x), int(mainY), self.color)
- self.image.show()
- def createCircle(self):
- # create equation
- # get radius
- y = 0;
- x = self.radius
- err = 0;
- while (x >= y):
- self.pixels[int(self.middleX) + x, int(self.middleY) + y] = (
- int(self.middleX) + x, int(self.middleY) + y, 255)
- self.pixels[int(self.middleX) + y, int(self.middleY) + x] = (
- int(self.middleX) + y, int(self.middleY) + x, 255)
- self.pixels[int(self.middleX) - y, int(self.middleY) + x] = (
- int(self.middleX) - y, int(self.middleY) + x, 255)
- self.pixels[int(self.middleX) - x, int(self.middleY) + y] = (
- int(self.middleX) - x, int(self.middleY) + y, 255)
- self.pixels[int(self.middleX) - x, int(self.middleY) - y] = (
- int(self.middleX) - x, int(self.middleY) - y, 255)
- self.pixels[int(self.middleX) - y, int(self.middleY) - x] = (
- int(self.middleX) - y, int(self.middleY) - x, 255)
- self.pixels[int(self.middleX) + y, int(self.middleY) - x] = (
- int(self.middleX) + y, int(self.middleY) - x, 255)
- self.pixels[int(self.middleX) + x, int(self.middleY) - y] = (
- int(self.middleX) + x, int(self.middleY) - y, 255)
- if err <= 0:
- y = y + 1
- err = err + (2 * y + 1)
- else:
- x = x - 1
- err -= 2 * x + 1
- self.image.show()
- drawLine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement