PageRenderTime 16ms CodeModel.GetById 11ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Samples/BadPaint/demo_snippets.py

#
Python | 61 lines | 20 code | 8 blank | 33 comment | 4 complexity | 53d39668e6a99824f676f12007b7f414 MD5 | raw file
 1#####################################################################################
 2#
 3#  Copyright (c) Microsoft Corporation. All rights reserved.
 4#
 5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
 6# copy of the license can be found in the License.html file at the root of this distribution. If 
 7# you cannot locate the  Apache License, Version 2.0, please send an email to 
 8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 9# by the terms of the Apache License, Version 2.0.
10#
11# You must not remove this notice, or any other, from this software.
12#
13#
14#####################################################################################
15
16#------------------------------------------------------------------------------
17#--INSERTS RANDOM SQUARES
18
19from System.Windows.Controls import Canvas
20from System.Windows.Shapes import *
21from System.Windows.Media import *
22from System import Random
23rand = Random()
24
25for i in xrange(100):
26    rect = Rectangle(Width=20, Height=20, Fill=Brushes.Blue)
27    Application.Painting.Children.Add(rect)
28    Canvas.SetLeft(rect, rand.Next(Application.Painting.ActualWidth))
29    Canvas.SetTop(rect, rand.Next(Application.Painting.ActualHeight))
30
31
32#------------------------------------------------------------------------------
33#--INSERT A CIRCLE CONSISTING OF SQUARES OF DIFFERENT COLORS
34
35# setup circle
36from System.Windows.Controls import Canvas
37from System.Windows.Shapes import *
38from System.Windows.Media import *
39import math
40
41dim = min(Application.Painting.ActualWidth - 20, Application.Painting.ActualHeight - 20)/2
42for i, color in zip(xrange(0, 360, 10), dir(Brushes)):
43    rect = Rectangle(Width=20, Height=20, Fill=getattr(Brushes, color))
44    Application.Painting.Children.Add(rect)
45    Canvas.SetTop(rect, dim * math.sin(i * math.pi*2/360) + dim)
46    Canvas.SetLeft(rect, dim * math.cos(i * math.pi*2/360)+ dim)
47
48#--IRONRUBY CODE TO SPIN THE CIRCLE ABOVE
49# rotate
50#Canvas = System::Windows::Controls::Canvas
51#def callback
52#    self.application.painting.children.each do |child|
53#        top, left = Canvas.get_top(child), Canvas.get_left(child)
54#        run = (left - dim) / dim
55#        rise = (top - dim) / dim
56#        angle = Math.atan2 rise, run
57#        angle += Math::PI / 100
58#        Canvas.set_top child, dim * Math.sin(angle) + dim
59#        Canvas.set_left child, dim * Math.cos(angle)+ dim
60#    end
61#end