Friday, August 27, 2021

kv 43 perspective transform 2





#galaxy.py
from kivy.app import App
from kivy.graphics import Line
from kivy.metrics import dp
from kivy.properties import NumericProperty
from kivy.uix.widget import Widget


class Galaxy(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.n_vlines = 11
        self.n_hlines = 9
        self.space_vlines = dp(100)
        with self.canvas:
            self.vlines = []
            self.hlines = []
            for i in range(self.n_vlines):
                vline = Line(points=(0,0,0,100))
                self.vlines.append(vline)
            for j in range(self.n_hlines):
                hline = Line(points=(0,0,100,0))
                self.hlines.append(hline)

    def on_size(self, *args):
        start_x = self.center_x - int(self.n_vlines/2)*self.space_vlines
        for i in range(self.n_vlines):
            x = start_x+self.space_vlines*i
            x1, y1, x2, y2 = x, 0, x, self.height

            x1, y1 = self.perspective_transform(x1, y1)
            x2, y2 = self.perspective_transform(x2, y2)

            self.vlines[i].points = (x1, y1, x2, y2)

        space_hlines = self.height / self.n_hlines
        for j in range(self.n_hlines):
            y = j * space_hlines
            x1, y1, x2, y2 = start_x, y, self.width-start_x, y

            _, y1 = self.perspective_transform(x1, y1)
            x1, _ = self.perspective_transform(x1, y1/0.8)
            _, y2 = self.perspective_transform(x2, y2)
            x2, _ = self.perspective_transform(x2, y2/0.8)

            self.hlines[j].points = (x1, y1, x2, y2)

    def perspective_transform(self, x, y):
        #return x, y
        ratio = (self.height-y)/self.height
        tx = ratio*(self.center_x-x)
        ty = ratio**2
        return self.center_x-tx, 0.8*self.height*(1-ty)


class GalaxyApp(App):
    pass


GalaxyApp().run()

reference:

No comments:

Post a Comment