\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/alt/python27/lib64/python2.7/site-packages/mpl_toolkits/axisartist/

Viewing File: clip_path.py

import numpy as np
from math import degrees
import math

def atan2(dy, dx):
    if dx == 0 and dx == 0:
        print "warning"
        return 0
    else:
        return math.atan2(dy, dx)

# FIXME : The current algorithm seems to return incorrect angle when the line
# ends at the boudnary.

def clip(xlines, ylines, x0, clip="right"):

    clipped_xlines = []
    clipped_ylines = []

    _pos_angles = []

    for x, y in zip(xlines, ylines):

        if clip in ["up", "right"]:
            b = (x < x0).astype("i")
            db = b[1:] - b[:-1]
        else:
            b = (x > x0).astype("i")
            db = b[1:] - b[:-1]


        if b[0]:
            ns = 0
        else:
            ns = -1
        segx, segy = [], []
        for (i,) in np.argwhere(db!=0):
            c = db[i]
            if c == -1:
                dx = (x0 - x[i])
                dy = (y[i+1] - y[i]) * (dx/ (x[i+1] - x[i]))
                y0 = y[i] + dy
                clipped_xlines.append(np.concatenate([segx, x[ns:i+1], [x0]]))
                clipped_ylines.append(np.concatenate([segy, y[ns:i+1], [y0]]))
                ns = -1
                segx, segy = [], []

                if dx == 0. and dy == 0:
                    dx = x[i+1] - x[i]
                    dy = y[i+1] - y[i]

                a = degrees(atan2(dy, dx))
                _pos_angles.append((x0, y0, a))

            elif c == 1:
                dx = (x0 - x[i])
                dy = (y[i+1] - y[i]) * (dx / (x[i+1] - x[i]))
                y0 = y[i] + dy
                segx, segy = [x0], [y0]
                ns = i+1

                if dx == 0. and dy == 0:
                    dx = x[i+1] - x[i]
                    dy = y[i+1] - y[i]

                a = degrees(atan2(dy, dx))
                _pos_angles.append((x0, y0, a))

                #print x[i], x[i+1]

        if ns != -1:
            clipped_xlines.append(np.concatenate([segx, x[ns:]]))
            clipped_ylines.append(np.concatenate([segy, y[ns:]]))

        #clipped_pos_angles.append(_pos_angles)


    return clipped_xlines, clipped_ylines, _pos_angles


def clip_line_to_rect(xline, yline, bbox):

    x0, y0, x1, y1 = bbox.extents

    lx1, ly1, c_right_ = clip([xline], [yline], x1, clip="right")
    lx2, ly2, c_left_ = clip(lx1, ly1, x0, clip="left")
    ly3, lx3, c_top_ = clip(ly2, lx2, y1, clip="right")
    ly4, lx4, c_bottom_ = clip(ly3, lx3, y0, clip="left")

    #c_left = [((x, y), (a+90)%180-180) for (x, y, a) in c_left_ \
    #          if bbox.containsy(y)]
    c_left = [((x, y), (a+90)%180-90) for (x, y, a) in c_left_ \
              if bbox.containsy(y)]
    c_bottom = [((x, y), (90 - a)%180) for (y, x, a) in c_bottom_  \
                if bbox.containsx(x)]
    c_right = [((x, y), (a+90)%180+90) for (x, y, a) in c_right_ \
               if bbox.containsy(y)]
    c_top = [((x, y), (90 - a)%180+180) for (y, x, a) in c_top_ \
             if bbox.containsx(x)]

    return zip(lx4, ly4), [c_left, c_bottom, c_right, c_top]


if __name__ == "__main__":

    import matplotlib.pyplot as plt

    x = np.array([-3, -2, -1, 0., 1, 2, 3, 2, 1, 0, -1, -2, -3, 5])
    #x = np.array([-3, -2, -1, 0., 1, 2, 3])
    y = np.arange(len(x))
    #x0 = 2

    plt.plot(x, y, lw=1)

    from matplotlib.transforms import Bbox
    bb = Bbox.from_extents(-2, 3, 2, 12.5)
    lxy, ticks = clip_line_to_rect(x, y, bb)
    for xx, yy in lxy:
        plt.plot(xx, yy, lw=1, color="g")

    ccc = iter(["ro", "go", "rx", "bx"])
    for ttt in ticks:
        cc = ccc.next()
        for (xx, yy), aa in ttt:
            plt.plot([xx], [yy], cc)

    #xlim(