JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Computer graphics Line cliping code sbbb | JustPaste.app
2 days ago2 views
👨‍💻Programming

Computer graphics Line cliping code sbbb

import matplotlib
matplotlib.use('TKAgg')
import matplotlib.pyplot as plt


# represent line class as line =(start,end)
# line = {"start":(a,b), "end":(c,d)}


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

class Line:
    def __init__(self, start:Point ,end: Point):
        self.start=start
        self.end=end

class ViewWindow:
    def __init__(self,lower, higher):
        self.xmin, self.ymin = lower
        self.xmax, self.ymax = higher

class RegionBits:
    # region bits
    left = 0b0001
    right = 0b0010
    top = 0b1000
    bottom = 0b0100


def plot_line_and_window(window: ViewWindow, line: Line):
    plt.plot(
        [window.xmin, window.xmin, window.xmax, window.xmax, window.xmin],
        [window.ymin, window.ymax, window.ymax, window.ymin, window.ymin]
    )
    plt.plot(
        [line.start.x , line.end.x],
        [line.start.y , line.end.y]
    )
    # return plt.gca()
    plt.axis('equal')
    plt.show()



def set_encode(point: Point, window:ViewWindow):
    # T B R L format
    # region bits
    left = 0b0001
    right = 0b0010
    top = 0b1000
    bottom = 0b0100

    code = 0b0000 # inside

    #top
    if(point.y > window.ymax):
        code |= top
    #bottom
    if(point.y < window.ymin):
        code |= bottom
    #right
    if(point.x > window.xmax):
        code |= right
    #left
    if(point.x < window.xmin):
        code |= left

    return code



def do_clipping(line: Line, window: ViewWindow):
    ecode1 = set_encode(line.start, window)
    ecode2 = set_encode(line.end, window)

    slope = (line.end.y-line.start.y)/(line.end.x-line.start.x) 

    newpoint=Point()

    # 0000 inside the voewpoint
    if ecode1 | ecode2 == 0:
        print(" fully inside viewpoint")

    elif ecode1 & ecode2 != 0:
        print("completely outside")

    else:
        for ecode in [ecode1, ecode2]:
            # if any code is top    1000 x,y=(x',ymax)
            if ecode == 0b1000:
                y=viewpoint.ymax
                x=line.start.x + slope*(line.end.x - line.start.x)
            # if any code is bottom 0010 x,y=(x',ymin)
            if ecode == 0b0010:
                y=viewpoint.ymin
                x=line.start.x + slope*(line.end.x - line.start.x)
            # if any code is right  1000 x,y=(xmax,y')
            if ecode == 0b1000:
                x=viewpoint.xmax
                y=line.start.y + (1/slope)
            # if any code is left   1000 x,y=(xmin,y')

                newpoint = Point(x,y)



if __name__=="__main__":
    # l= Line(Point(1,2), Point(5,5))
    # v= ViewWindow((2,2),(5,4))
    # code1 = set_encode(l.start, v)
    # code2 = set_encode(l.end, v)
    # print(f"{code1:4b}, {code2:4b}")
    # plot_line_and_window(v,l)

    # l= Line(Point(3,3), Point(4,4))
    # v= ViewWindow((2,2),(5,4))

    # code1 = set_encode(l.start, v)
    # code2 = set_encode(l.end, v)
    # print(f"{code1:4b}, {code2:4b}")
    # plot_line_and_window(v,l)


    l= Line(Point(-3,1), Point(1,2))
    v= ViewWindow((2,2),(5,4))

    code1 = set_encode(l.start, v)
    code2 = set_encode(l.end, v)
    print(f"{code1:4b}, {code2:4b}")
    plot_line_and_window(v,l)
← Back to timeline