#!/usr/bin/env python from __future__ import division import gtk import math def B(x,y,z): return (1-y/2, x/2-1, 0) def E(x,y,z): return (x/2-1, y/2-1, 0) def repaint(widget, event): window = widget.window GC = window.new_gc() def get_a_angle(x1,y1,x2,y2): l = math.sqrt((x2-x1)**2+(y2-y1)**2) if l == 0: return None #(x2-y1,y2-y1) inproduct (1, 0)=(cos a)*l return math.acos((x2-x1)/l) * 360*64 / (math.pi * 2) def draw_line(x1,y1,x2,y2): window.draw_line(GC, x1 * 50,300 - y1 * 50,x2 * 50,300 - y2 * 50) def draw_arrow(x1,y1,x2,y2): draw_line(x1,y1,x2,y2) angle = get_a_angle(x1,y1,x2,y2) if angle is not None: start_angle = angle - 64 * 60 end_angle = angle + 64 * 60 else: start_angle = 0 end_angle = 360 * 64 window.draw_arc(GC, False, x2 * 50 - 5, 300 - y2 * 50 - 5, 10, 10, start_angle, end_angle) for x in [1,2,3]: GC.set_rgb_fg_color(gtk.gdk.Color(0, 0, 0xF000)) draw_arrow(x, -5, x, +5) for y in [1,2,3]: GC.set_rgb_fg_color(gtk.gdk.Color(0, 0, 0xF000)) draw_arrow(-5, y, 5, y) GC.set_rgb_fg_color(gtk.gdk.Color(0, 0, 0)) B1 = B(x,y,0) E1 = E(x,y,0) GC.line_width = 2 draw_arrow(x, y, x + E1[0], y + E1[1]) GC.set_rgb_fg_color(gtk.gdk.Color(0x8000, 0, 0)) draw_arrow(x, y, x + B1[0], y + B1[1]) GC.line_width = 1 drawing_area = gtk.DrawingArea() drawing_area.connect("expose-event", repaint) drawing_area.show() window = gtk.Window() window.connect("delete-event", gtk.main_quit) window.add(drawing_area) window.show() gtk.main()