Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. from PIL import Image
  2. from math import *
  3.  
  4.  
  5. class drawLine:
  6. def __init__(self):
  7. self.color = 255
  8. command = raw_input()
  9. if command == "draw line":
  10. self.point_one_x = float(input("First point - X: "))
  11. self.point_one_y = float(input("First point - y: "))
  12. self.point_two_x = float(input("Second point - x: "))
  13. self.point_two_y = float(input("Second point - y: "))
  14. self.createCanvas()
  15. self.countPoints()
  16. elif command == "draw circle":
  17. self.middleX = int(input("Middle X:"))
  18. self.middleY = int(input("Middle Y:"))
  19. self.radius = int(input("Radius:"))
  20. self.createCanvas()
  21. self.createCircle()
  22.  
  23. def createCanvas(self):
  24. self.image = Image.new("RGB", (255, 255), "white")
  25. self.pixels = self.image.load()
  26.  
  27. def countPoints(self):
  28. # get equations
  29. y = abs(int(self.point_one_y) - int(self.point_two_y))
  30. x = abs(int(self.point_one_x) - int(self.point_two_x))
  31. if (self.point_two_x - self.point_one_x) != 0:
  32. a = (self.point_two_y + self.point_one_y) / (self.point_two_x + self.point_one_x)
  33. b = self.point_one_y - a * self.point_one_x
  34. print(a)
  35. print(b)
  36. if (y > x):
  37. if self.point_one_y > self.point_two_y:
  38. for mainY in range(int(self.point_two_y), int(self.point_one_y)):
  39. x = (mainY - b) / a
  40. self.pixels[int(x), int(mainY)] = (int(x), int(mainY), self.color)
  41. else:
  42. for mainY in range(int(self.point_one_y), int(self.point_two_y)):
  43. x = (mainY - b) / a
  44. self.pixels[int(x), int(mainY)] = (int(x), int(mainY), self.color)
  45. else:
  46. if self.point_one_x > self.point_two_x:
  47. for mainX in range(int(self.point_two_x), int(self.point_one_x)):
  48. y = a * mainX + b
  49. self.pixels[int(mainX), int(y)] = (int(mainX), int(y), self.color)
  50. else:
  51. for mainX in range(int(self.point_one_x), int(self.point_two_x)):
  52. y = a * mainX + b
  53. self.pixels[int(mainX), int(y)] = (int(mainX), int(y), self.color)
  54. else:
  55. a = self.point_two_y / self.point_two_x
  56. b = 0
  57. if self.point_one_y > self.point_two_y:
  58. for mainY in range(int(self.point_two_y), int(self.point_one_y)):
  59. self.pixels[int(self.point_two_x), int(mainY)] = (int(self.point_two_x), int(mainY), self.color)
  60. else:
  61. for mainY in range(int(self.point_one_y), int(self.point_two_y)):
  62. self.pixels[int(self.point_two_x), int(mainY)] = (int(self.point_two_x), int(mainY), self.color)
  63.  
  64. self.image.show()
  65.  
  66. def createCircle(self):
  67. # create equation
  68. # get radius
  69.  
  70.  
  71. y = 0;
  72. x = self.radius
  73. err = 0;
  74. while (x >= y):
  75. self.pixels[int(self.middleX) + x, int(self.middleY) + y] = (
  76. int(self.middleX) + x, int(self.middleY) + y, 255)
  77. self.pixels[int(self.middleX) + y, int(self.middleY) + x] = (
  78. int(self.middleX) + y, int(self.middleY) + x, 255)
  79. self.pixels[int(self.middleX) - y, int(self.middleY) + x] = (
  80. int(self.middleX) - y, int(self.middleY) + x, 255)
  81. self.pixels[int(self.middleX) - x, int(self.middleY) + y] = (
  82. int(self.middleX) - x, int(self.middleY) + y, 255)
  83. self.pixels[int(self.middleX) - x, int(self.middleY) - y] = (
  84. int(self.middleX) - x, int(self.middleY) - y, 255)
  85. self.pixels[int(self.middleX) - y, int(self.middleY) - x] = (
  86. int(self.middleX) - y, int(self.middleY) - x, 255)
  87. self.pixels[int(self.middleX) + y, int(self.middleY) - x] = (
  88. int(self.middleX) + y, int(self.middleY) - x, 255)
  89. self.pixels[int(self.middleX) + x, int(self.middleY) - y] = (
  90. int(self.middleX) + x, int(self.middleY) - y, 255)
  91.  
  92. if err <= 0:
  93. y = y + 1
  94. err = err + (2 * y + 1)
  95. else:
  96. x = x - 1
  97. err -= 2 * x + 1
  98.  
  99. self.image.show()
  100.  
  101.  
  102. drawLine()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement