首先,非常感谢您!多亏了你的建议,我已经解决了我的烦恼。
下面你可以看到一个简单的_self_containing_演示应用程序:
import gtk
from gtk import gdk
class Canvas(gtk.DrawingArea):
# the list of points is passed as argument
def __init__(self, points):
super(Canvas, self).__init__()
self.points = points
# Canvas is 800*500 only!
self.set_size_request(800, 500)
self.scaleFactor = 0.0
self.connect("expose_event", self.expose)
# Method to paint polygons and lines on screen
def expose(self, widget, event):
rect = widget.get_allocation()
w = rect.width
h = rect.height
# Calculation of the coordinates limits
xMax = max([c[0] for c in self.points])
yMax = max([c[1] for c in self.points])
xMin = min([c[0] for c in self.points])
yMin = min([c[1] for c in self.points])
# Calculation of the size of the bounding box
maxRectWidth = xMax - xMin
maxRectHeigth = yMax - yMin
# Scale factor calculation
width_ratio = float(w) / maxRectWidth
height_ratio = float(h) / maxRectHeigth
self.scaleFactor = min(height_ratio, width_ratio)
# Create Cairo Context object
ctx = widget.window.cairo_create()
# Set colour and line width
ctx.set_line_width(7)
ctx.set_source_rgb(255, 0, 0)
ctx.save()
# CRITICAL LINE: the Cairo Context is moved and scaled here
ctx.scale(self.scaleFactor, self.scaleFactor)
ctx.translate(-xMin, -yMin)
# Drawing of the lines
for i in range(0, len(self.points)):
currPoint = self.points[i]
currX = float(currPoint[0])
currY = float(currPoint[1])
nextIndex = i + 1
if (nextIndex == len(self.points)):
continue
nextPoint = self.points[nextIndex]
nextX = float(nextPoint[0])
nextY = float(nextPoint[1])
ctx.move_to(currX, currY)
ctx.line_to(nextX, nextY)
ctx.restore()
ctx.close_path()
ctx.stroke()
# a list of single points with large _LARGE _coordinates
li1 = [(200000,200000), (400000,200000), (400000,400000), (200000,400000)]
window = gtk.Window()
canvas = Canvas(li1)
window.add(canvas)
window.show_all()
gtk.main()