Graphics
The graphics module provides a number of classes to provide simple visualization capabilities. To use it, you first need to import the module:
import graphics
The Graphics class acts as an abstract container for graphical information; to actually launch the display see the Show class. You can create an empty scene like this,
var g = Graphics()
Additional elements can be added using the display method.
g.display(element)
Morpho provides the following fundamental Graphical element classes:
TriangleComplex
You can also use functions like Arrow, Tube and Cylinder to create these elements conveniently.
To combine graphics objects, use the add operator:
var g1 = Graphics(), g2 = Graphics()
// ...
Show(g1+g2)
Show
Show is used to launch an interactive graphical display using the external morphoview application. Show takes a Graphics object as an argument:
var g = Graphics()
Show(g)
TriangleComplex
A TriangleComplex is a graphical element that can be used as part of a graphical display. It consists of a list of vertices and a connectivity matrix that selects which vertices are used in each triangle.
To create one, call the constructor with the following arguments:
TriangleComplex(position, normals, colors, connectivity)
positionis aMatrixcontaining vertex positions as columns.normalsis aMatrixwith a normal for each vertex.colorsis the color of the object.connectivityis aSparsematrix where each column represents a triangle and rows correspond to vertices.
You can also provide optional arguments:
transmitsets the transparency of the object. This parameter is only used by the povray module as of now. Default is 0.filtersets the transparency of the object using a filter effect. This parameter is only used by the povray module as of now. Default is 0. For the difference betweentransmitandfilter, checkout the POVRay documentation.
Add to a Graphics object using the display method.
Arrow
The Arrow function creates an arrow. It takes two arguments:
arrow(start, end)
startandendare the two vertices. The arrow pointsstart->end.
You can also provide optional arguments:
aspectratiocontrols the width of the arrow relative to its lengthnis an integer that controls the quality of the display. Highernleads to a rounder arrow.coloris the color of the arrow. This can be a list of RGB values or aColorobjecttransmitsets the transparency of the arrow. This parameter is only used by the povray module as of now. Default is 0.filtersets the transparency of the arrow using a filter effect. This parameter is only used by the povray module as of now. Default is 0. For the difference betweentransmitandfilter, checkout the POVRay documentation.
Display an arrow:
var g = Graphics([])
g.display(Arrow([-1/2,-1/2,-1/2], [1/2,1/2,1/2], aspectratio=0.05, n=10))
Show(g)
Cylinder
The Cylinder function creates a cylinder. It takes two required arguments:
cylinder(start, end)
startandendare the two vertices.
You can also provide optional arguments:
aspectratiocontrols the width of the cylinder relative to its length.nis an integer that controls the quality of the display. Highernleads to a rounder cylinder.coloris the color of the cylinder. This can be a list of RGB values or aColorobject.transmitsets the transparency of the cylinder. This parameter is only used by the povray module as of now. Default is 0.filtersets the transparency of the cylinder using a filter effect. This parameter is only used by the povray module as of now. Default is 0. For the difference betweentransmitandfilter, checkout the POVRay documentation.
Display an cylinder:
var g = Graphics()
g.display(Cylinder([-1/2,-1/2,-1/2], [1/2,1/2,1/2], aspectratio=0.1, n=10))
Show(g)
Tube
The Tube function connects a sequence of points to form a tube.
Tube(points, radius)
pointsis a list of points; this can be a list of lists or aMatrixwith the positions as columns.radiusis the radius of the tube.
You can also provide optional arguments:
nis an integer that controls the quality of the display. Highernleads to a rounder tube.coloris the color of the tube. This can be a list of RGB values or aColorobject.closedis aboolthat indicates whether the tube should be closed to form a loop.transmitsets the transparency of the tube. This parameter is only used by the povray module as of now. Default is 0.filtersets the transparency of the tube using a filter effect. This parameter is only used by the povray module as of now. Default is 0. For the difference betweentransmitandfilter, checkout the POVRay documentation.
Draw a square:
var a = Tube([[-1/2,-1/2,0],[1/2,-1/2,0],[1/2,1/2,0],[-1/2,1/2,0]], 0.1, closed=true)
var g = Graphics()
g.display(a)
Sphere
The Sphere function creates a sphere.
Sphere(center, radius)
centeris the position of the center of the sphere; this can be a list or columnMatrix.radiusis the radius of the sphere
You can also provide optional arguments:
coloris the color of the sphere. This can be a list of RGB values or aColorobject.transmitsets the transparency of the sphere. This parameter is only used by the povray module as of now. Default is 0.filtersets the transparency of the sphere using a filter effect. This parameter is only used by the povray module as of now. Default is 0. For the difference betweentransmitandfilter, checkout the POVRay documentation.
Draw some randomly sized spheres:
var g = Graphics()
for (i in 0...10) {
g.display(Sphere([random()-1/2, random()-1/2, random()-1/2], 0.1*(1+random()), color=Gray(random())))
}
Show(g)
Text
A Text object is used to display text.
Text(text, position)
textis the text to display as a string.positionis the position at which to display the text.
You can also provide optional arguments:
coloris the color of the text. This should be aColorobject.dirnis the direction along which the text is drawn. This should be aListor aMatrix.sizeis the font size to useverticalis the vertical direction for the textfontis theFontobject to use.
Draw several pieces of text around the y axis:
var g = Graphics()
for (phi in 0..Pi:Pi/8) {
g.display(Text("Hello World", [0,0,0], size=72, dirn=[0,1,0], vertical=[cos(phi),0,sin(phi)]))
}
Show(g)