TI/Klasy - ćwiczenia: Różnice pomiędzy wersjami

Z Brain-wiki
(Utworzono nową stronę "<source lang="python"> #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Apr 16 12:49:54 2019 @author: tgub """ import numpy as np import pylab as py ta...")
 
 
Linia 34: Linia 34:
 
                 x= slope*(y-self.a.y)+self.a.x
 
                 x= slope*(y-self.a.y)+self.a.x
 
                 tablica[int(round(x)),int(round(y))]=1
 
                 tablica[int(round(x)),int(round(y))]=1
 +
    def dlugosc(self):
 +
        return ((self.b.x-self.a.x)**2+(self.b.y-self.a.y)**2)**(0.5)
 
                  
 
                  
 
class Wielokat():
 
class Wielokat():
 
     def __init__(self,*args):
 
     def __init__(self,*args):
 
         self.points=args
 
         self.points=args
 +
        self.odcinki=[Odcinek(self.points[n],self.points[(n+1)%len(self.points)]) for n in range(len(self.points))]
 
     def rysuj(self,tab):
 
     def rysuj(self,tab):
         for n in range(len(self.points)):
+
         for odc in self.odcinki:
            Odcinek(self.points[n],self.points[(n+1)%len(self.points)]).rysuj(tab)
+
            odc.rysuj(tab)
       
+
    def obwod(self):
 +
        return sum([odc.dlugosc() for odc in self.odcinki])
  
k=Wielokat(Punkt(10,10),Punkt(20,90),Punkt(60,70),Punkt(90,15))
+
k=Wielokat(Punkt(10,10),Punkt(40,10),Punkt(10,50))
 
k.rysuj(tab)
 
k.rysuj(tab)
 
py.imshow(tab,cmap = py.cm.gray, interpolation = 'nearest')
 
py.imshow(tab,cmap = py.cm.gray, interpolation = 'nearest')
 
py.show()
 
py.show()
 
+
print(k.obwod())
  
 
</source>
 
</source>

Aktualna wersja na dzień 11:38, 16 kwi 2019

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 16 12:49:54 2019

@author: tgub
"""
import numpy as np
import pylab as py
tab=np.zeros((100,100))


class Punkt():
    def __init__(self,x,y):
        self.x=x
        self.y=y

class Odcinek():
    def __init__(self,a,b):
        self.a=a
        self.b=b
    def rysuj(self,tablica):
        dx=(self.b.x-self.a.x)
        dy=(self.b.y-self.a.y)
        if abs(dx)>=abs(dy):
            slope=dy/dx
            for x in np.linspace(self.a.x,self.b.x,abs(dx)+1):
                y= slope*(x-self.a.x)+self.a.y
                tablica[int(round(x)),int(round(y))]=1
        else:
            slope=dx/dy
            for y in np.linspace(self.a.y,self.b.y,abs(dy)+1):
                x= slope*(y-self.a.y)+self.a.x
                tablica[int(round(x)),int(round(y))]=1
    def dlugosc(self):
        return ((self.b.x-self.a.x)**2+(self.b.y-self.a.y)**2)**(0.5)
                
class Wielokat():
    def __init__(self,*args):
        self.points=args
        self.odcinki=[Odcinek(self.points[n],self.points[(n+1)%len(self.points)]) for n in range(len(self.points))]
    def rysuj(self,tab):
        for odc in self.odcinki:
            odc.rysuj(tab)
    def obwod(self):
        return sum([odc.dlugosc() for odc in self.odcinki])

k=Wielokat(Punkt(10,10),Punkt(40,10),Punkt(10,50))
k.rysuj(tab)
py.imshow(tab,cmap = py.cm.gray, interpolation = 'nearest')
py.show()
print(k.obwod())