Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Oversett programmet til Rects.java, basert på rpncalc1.py oversatt til Java.

MATLAB-brukere: Se tilsvarende eksempel under MATLAB-Java-oversetting - rects-oppgave.

Code Block
languagepython
titlerects.py
collapsetrue
# rects.py
r1x1 = 0
r1y1 = 0
r1x2 = 0
r1y2 = 0

r2x1 = 0
r2y1 = 0
r2x2 = 0
r2y2 = 0

def intervalsOverlap(n1, n2, m1, m2):
    return not (n1 > m2 or n2 < m1)

def rectanglesOverlap():
    return intervalsOverlap(r1x1, r1x2, r2x1, r2x2) and intervalsOverlap(r1y1, r1y2, r2y1, r2y2)

def rectangle2String(x1, y1, x2, y2):
    return "(" + str(x1) + "," + str(y1) + ")" + "," + "(" + str(x2) + "," + str(y2) + ")"

def main():
    while (True):
        print("Rect1: " + rectangle2String(r1x1, r1y1, r1x2, r1y2))
        print("Rect2: " + rectangle2String(r2x1, r2y1, r2x2, r2y2))
        token = raw_input(" > ")
        if token == "overlaps?":
            print(rectanglesOverlap())
        elif token == "exit":
            break
        else:
            pos = token.find("=")
            if pos >= 4:
                val = float(token[pos + 1 :])
                if token.startswith("r1x1"):
                    r1x1 = val
                elif token.startswith("r1y1"):
                    r1y1 = val
                elif token.startswith("r1x2"):
                    r1x2 = val
                elif token.startswith("r1y2"):
                    r1y2 = val
                elif token.startswith("r2x1"):
                    r2x1 = val
                elif token.startswith("r2y1"):
                    r2y1 = val
                elif token.startswith("r2x2"):
                    r2x2 = val
                elif token.startswith("r2y2"):
                    r2y2 = val

...