from dataclasses import dataclass
from typing import *

@dataclass
class Len:
    px: int = 0
    perc: int = 0
    
    def __add__(self, other):
        return Len(self.px + other.px, self.perc + other.perc)
        
@dataclass
class Pos:
    x: int = 0
    y: int = 0
    
    def __add__(self, other):
        return Pos(self.x + other.x, self.y + other.y)
        
@dataclass
class Trans:
    base: Pos = Pos()
    delta: Pos = Pos()
    size: Pos = Pos()
    scale: Pos = Pos(1, 1)
    
    def get_size(self, x, y):
        x = (x.perc * self.size.x)/100 + x.px
        y = (y.perc * self.size.y)/100 + y.px
        return x * self.scale.x, y * self.scale.y
        
    def get_local(self, x, y):
        x, y = self.get_size(x, y)
        return x + self.delta.x, y + self.delta.y
        
    def get_global(self, x, y):
        x, y = self.get_local(x, y)
        return x + self.base.x, y + self.base.y
        
class Graphics:
    def __post_init__(self):
        self.border = None
        self.trans = Trans()
        
    def render(self, render, local = False):
        pos = self.to_local() if local else self.to_global()
        method = getattr(render, type(self).__name__)
        method(pos=pos, col=self.color, border=self.border)
    
    def mouse_at(self, x, y, mouse_type):
        name = f"on_{mouse_type}"
        if not self.is_inside(x, y):
            name = f"{name}_out"
        if hasattr(self, name):
            getattr(self, name)(x, y)
            
    def key_at(self, key, key_type):
        if hasattr(self, f"on_key_{key_type}"):
            getattr(self, f"on_key_{key_type}")(key)
        if hasattr(self, f"on_{key}_{key_type}"):
            getattr(self, f"on_{key}_{key_type}")()
      
@dataclass
class Rect(Graphics):
    x: int
    y: int
    width: int
    height: int
    color: str
    
    def move(self, x, y):
        self.x = self.x + x
        self.y = self.y + y
        
    def addsize(self, width, height):
        self.width = self.width + width
        self.height = self.height + height
        
    def to_local(self):
        pos = self.trans.get_local(self.x, self.y)
        size = self.trans.get_size(self.width, self.height)
        return [pos, size]
        
    def to_global(self):
        pos = self.trans.get_global(self.x, self.y)
        size = self.trans.get_size(self.width, self.height)
        return [pos, size]
        
    def is_inside(self, x, y):
        pos = self.to_global()
        x_proj = pos[0][0] <= x <= pos[0][0] + pos[1][0]
        y_proj = pos[0][1] <= y <= pos[0][1] + pos[1][1]
        return x_proj and y_proj
        
@dataclass
class Line(Graphics):
    pos: List[Pos]
    color: str
    
    def move_point(self, i, x, y):
        self.pos[i] = self.pos[i] + Pos(x, y)
        
    def move(self, x, y):
        for i in range(len(self.pos)):
            self.move_point(i, x, y)
            
    def to_local(self):
        result = []
        for pos in self.pos:
            result.append(self.trans.get_local(pos.x, pos.y))
        return result
        
    def to_global(self):
        result = []
        for pos in self.pos:
            result.append(self.trans.get_global(pos.x, pos.y))
        return result
        
    def cross(x1, y1, x2, y2, x, y):
        return (x-x1)*abs(y2-y1) - (y-y1)*abs(x2-x1)
        
    def incross(x1, y1, x2, y2, x, y):
        return -600 < Line.cross(x1, y1, x2, y2, x, y) < 600
        
    def is_inside(self, x, y):
        pos = self.to_global()
        for i in range(len(pos) - 1):
            x1, y1 = pos[i]
            x2, y2 = pos[i+1]
            if Line.incross(x1, y1, x2, y2, x, y) and x1 < x < x2 and y1 < y < y2:
              return True
        return False

@dataclass
class Ellipse(Rect):
    def is_inside(self, x, y):
        (x_pos, y_pos), (width, height) = self.to_global()
        rad_x = 1/2*width
        rad_y = 1/2*height
        return (x-(x_pos+rad_x))**2/rad_x + (y-(y_pos+rad_y))**2/rad_y <= 1
        
@dataclass
class Polygon(Line):
    pass
        
@dataclass
class ContentShape(Rect):
    src: str
    
    def __post_init__(self):
        super().__post_init__()
        self.content = None
        
class Handler:
    def on_mouse_move(self, x, y):
        print("MyLine inside", x, y)
        
    def on_mouse_move_out(self, x, y):
        print("MyRect/Line Out", x, y)
        
    def on_key_down(self, key):
        print("onclicked", key)
        
    def on_a_down(self):
        print("on a down")
        
class MyRect(Rect, Handler):
    pass
  
class MyLine(Line, Handler):
    pass
        
class ConsoleRender:
    def unload(self, **kwargs):
        return kwargs["pos"], kwargs["col"], kwargs["border"]
      
    def Rect(self, **kwargs):
        print("Rect: ", *self.unload(**kwargs))
        
    def Line(self, **kwargs):
        print("Line: ", *self.unload(**kwargs))
        
    def Ellipse(self, **kwargs):
        print("Ellipse: ", *self.unload(**kwargs))
        
    def Polygon(self, **kwargs):
        print("Polygon: ", *self.unload(**kwargs))
      
c = ConsoleRender()
g = Line([Pos(Len(3, 2), Len(11, 2)), Pos(Len(8, 7), Len(3, 9)), Pos(Len(12, 7), Len(22, 3))], "purple")
g.trans = Trans(Pos(2, 8), Pos(3, 2), Pos(197, 136), Pos(3, 2))
print(g.to_local())
print(g.to_global())
g.render(c)
print(g.is_inside(50, 40))
print(Line.cross(25.82, 37.44, 82.37, 62.16, 70, 50))
print(Line.cross(35.4, 249.64, 44.6, 110.92, 35.4, 249.64)) 
by

Python Online Compiler

Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.

Taking inputs (stdin)

OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.

import sys
name = sys.stdin.readline()
print("Hello "+ name)

About Python

Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.

Tutorial & Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition IF-ELSE is used.

if conditional-expression
    #code
elif conditional-expression
    #code
else:
    #code

Note:

Indentation is very important in Python, make sure the indentation is followed correctly

2. For:

For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.

Example:

mylist=("Iphone","Pixel","Samsung")
for i in mylist:
    print(i)

3. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while condition  
    #code 

Collections

There are four types of collections in Python.

1. List:

List is a collection which is ordered and can be changed. Lists are specified in square brackets.

Example:

mylist=["iPhone","Pixel","Samsung"]
print(mylist)

2. Tuple:

Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.

Example:

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)

Below throws an error if you assign another value to tuple again.

myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)

3. Set:

Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.

Example:

myset = {"iPhone","Pixel","Samsung"}
print(myset)

4. Dictionary:

Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.

Example:

mydict = {
    "brand" :"iPhone",
    "model": "iPhone 11"
}
print(mydict)

Supported Libraries

Following are the libraries supported by OneCompiler's Python compiler

NameDescription
NumPyNumPy python library helps users to work on arrays with ease
SciPySciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation
SKLearn/Scikit-learnScikit-learn or Scikit-learn is the most useful library for machine learning in Python
PandasPandas is the most efficient Python library for data manipulation and analysis
DOcplexDOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling