Skip to content

Instantly share code, notes, and snippets.

@ilkermanap
Created May 3, 2020 17:57
Show Gist options
  • Save ilkermanap/2ccfa23cf489e7bdc002a8481943ae02 to your computer and use it in GitHub Desktop.
Save ilkermanap/2ccfa23cf489e7bdc002a8481943ae02 to your computer and use it in GitHub Desktop.
Nokta, cizgi ve poligon nesneleri
from math import sqrt
class Nokta:
def __init__(self, x,y):
self.x1 = x
self.y1 = y
def __str__(self):
return f"{self.x1},{self.y1}"
class Cizgi(Nokta):
def __init__(self, x1,y1,x2,y2):
Nokta.__init__(self, x1,y1)
self.x2 = x2
self.y2 = y2
def start(self):
return Nokta(self.x1,self.y1)
def stop(self):
return Nokta(self.x2,self.y2)
def uzunluk(self):
return sqrt( abs(self.x2-self.x1)**2 + abs(self.y2-self.y1))
def __str__(self):
return f" ({str(self.start())} -> {str(self.stop())}) "
class Poligon:
def __init__(self):
self.start = None
self.vertex = []
def uzunluk(self):
f = 0
for vertex in self.vertex:
f += vertex.uzunluk()
return f
def parca_ekle(self, parca):
if type(parca) is Nokta:
if self.start is None:
self.start = parca
else:
son_vertex = self.vertex[-1]
self.vertex.append(Cizgi(son_vertex.x2,son_vertex.y2, parca.x1, parca.y1))
if type(parca) is Cizgi:
if self.start is None:
self.start = Nokta(parca.x1,parca.y1)
self.vertex.append(parca)
else:
if len(self.vertex) == 0:
self.vertex.append(Cizgi(self.start.x1,self.start.y1,parca.x1,parca.y1))
self.vertex.append(parca)
def __str__(self):
s = "Poligon ("
for vertex in self.vertex:
s += str(vertex) + ","
s = s[:-1] + ")"
return s
if __name__ == "__main__":
p = Poligon()
p.parca_ekle(Nokta(0,0))
p.parca_ekle(Cizgi(0,0,10,0 ))
print(str(p))
print("uzunlugu :", p.uzunluk())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment