Download 2.9.2 Two-Dimensional Graphics Elements

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

General-purpose computing on graphics processing units wikipedia , lookup

Free and open-source graphics device driver wikipedia , lookup

2.5D wikipedia , lookup

BSAVE (bitmap format) wikipedia , lookup

Waveform graphics wikipedia , lookup

Framebuffer wikipedia , lookup

Apple II graphics wikipedia , lookup

Video card wikipedia , lookup

InfiniteReality wikipedia , lookup

Tektronix 4010 wikipedia , lookup

Graphics processing unit wikipedia , lookup

Molecular graphics wikipedia , lookup

Transcript
2. Principles of Mathematica
462
2.9.2 Two-Dimensional Graphics Elements
Point {x, y}]
point at position x, y
Line {{x1 , y1 }, {x2 , y2 }, ... }]
line through the points {x1 , y1 }, {x2 , y2 }, ...
Rectangle {xmin, ymin}, {xmax, ymax}]
filled rectangle
Polygon {{x1 , y1 }, {x2 , y2 }, ... }]
filled polygon with the specified list of corners
Circle {x, y}, r]
Disk {x, y}, r]
circle with radius r centered at x, y
filled disk with radius r centered at x, y
Raster {{a11 , a12 , ... }, {a21 , ... }, ... }]
rectangular array of gray levels between 0 and 1
Text expr, {x, y}]
the text of expr, centered at x, y (see Section 2.9.16)
Basic two-dimensional graphics elements.
Here is a line primitive.
In 1]:= sawline = LineTable{n, (-1)^n}, {n, 6}]]
Out 1]= Line {{1, -1}, {2, 1}, {3, -1}, {4, 1}, {5, -1},
{6, 1}}]
This shows the line as a
two-dimensional graphics object.
In 2]:= sawgraph = Show Graphicssawline] ]
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2.9 The Structure of Graphics and Sound
This redisplays the line, with axes
added.
463
In 3]:= Show %, Axes -> True ]
1
0.5
2
4
3
5
6
-0.5
-1
You can combine graphics objects that you have created explicitly from graphics primitives with ones
that are produced by functions like Plot.
This produces an ordinary Mathematica
plot.
In 4]:= PlotSinPi x], {x, 0, 6}]
1
0.5
1
2
3
4
5
6
3
4
5
6
-0.5
-1
This combines the plot with the
sawtooth picture made above.
In 5]:= Show%, sawgraph]
1
0.5
1
2
-0.5
-1
You can combine different graphical elements simply by giving them in a list. In two-dimensional
graphics, Mathematica will render the elements in exactly the order you give them. Later elements are
therefore effectively drawn on top of earlier ones.
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2. Principles of Mathematica
464
Here is a list of two Rectangle graphics
elements.
In 6]:= {Rectangle{1, -1}, {2, -0.6}],
Rectangle{4, .3}, {5, .8}]}
Out 6]= {Rectangle {1, -1}, {2, -0.6}],
Rectangle {4, 0.3}, {5, 0.8}]}
This draws the rectangles on top of the
line that was defined above.
In 7]:= Show Graphics {sawline, %} ]]
The Polygon graphics primitive takes a list of x, y coordinates, corresponding to the corners of a
polygon. Mathematica joins the last corner with the first one, and then fills the resulting area.
Here are the coordinates of the corners
of a regular pentagon.
In 8]:= pentagon = Table{Sin2 Pi n/5], Cos2 Pi n/5]}, {n, 5}]
2 Pi
2 Pi
4 Pi
4 Pi
Out 8]= {{Sin -------], Cos -------]}, {Sin -------], Cos -------]},
5
5
5
5
6 Pi
6 Pi
8 Pi
8 Pi
{Sin -------], Cos -------]}, {Sin -------], Cos -------]}, {0, 1}}
5
5
5
5
This displays the pentagon. With the
default choice of aspect ratio, the
pentagon looks somewhat squashed.
In 9]:= Show Graphics Polygonpentagon] ] ]
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2.9 The Structure of Graphics and Sound
465
This chooses the aspect ratio so that the
shape of the pentagon is preserved.
In 10]:= Show%, AspectRatio -> Automatic]
Mathematica can handle polygons which
fold over themselves.
In 11]:= ShowGraphics
Circle {x, y}, r]
Circle {x, y}, {rx , ry }]
Polygon {{-1, -1}, {1, 1}, {1, -1}, {-1, 1}} ] ]]
a circle with radius r centered at the point {x, y}
an ellipse with semi-axes rx and ry
Circle {x, y}, r, {theta1 , theta2 }]
a circular arc
Circle {x, y}, {rx , ry }, {theta1 , theta2 }]
an elliptical arc
Disk {x, y}, r], etc.
filled disks
Circles and disks.
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2. Principles of Mathematica
466
This shows two circles with radius 2.
Setting the option
AspectRatio -> Automatic makes the
circles come out with their natural
aspect ratio.
In 12]:= Show Graphics
This shows a sequence of disks with
progressively larger semi-axes in the x
direction, and progressively smaller
ones in the y direction.
In 13]:= Show Graphics
{Circle{0, 0}, 2], Circle{1, 1}, 2]} ],
AspectRatio -> Automatic ]
TableDisk{3n, 0}, {n/4, 2-n/4}], {n, 4}] ],
AspectRatio -> Automatic ]
Mathematica allows you to generate arcs of circles, and segments of ellipses. These objects are specified by starting and finishing angles. Angles are measured counter-clockwise in radians with zero corresponding to the positive x direction. Angle measure always corresponds to circular geometry.
This draws a 140 wedge centered at the
origin.
In 14]:= Show Graphics Disk{0, 0}, 1, {0, 140 Degree}] ],
AspectRatio -> Automatic ]
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
2.9 The Structure of Graphics and Sound
467
Raster {{a11 , a12 , ... }, {a21 , ... }, ... }]
array of gray levels between 0 and 1
Raster array, {{xmin, ymin}, {xmax, ymax}}, {zmin, zmax}]
array of gray levels between zmin and zmax drawn in the
rectangle defined by {xmin, ymin} and {xmax, ymax}
RasterArray {{g11 , g12 , ... }, {g21 , ... }, ... }]
rectangular array of cells colored according to the graphics
directives gij
Raster-based graphics elements.
Here is a 4
and 1.
4 array of values between 0
In 15]:= modtab = TableModi, j]/3, {i, 4}, {j, 4}] // N
Out 15]= {{0, 0.333333, 0.333333, 0.333333},
{0, 0, 0.666667, 0.666667}, {0, 0.333333, 0, 1.},
{0, 0, 0.333333, 0}}
This uses the array of values as gray
levels in a raster.
In 16]:= Show Graphics Raster%] ] ]
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.
468
This shows two overlapping copies of
the raster.
2. Principles of Mathematica
In 17]:= Show Graphics {Rastermodtab, {{0, 0}, {2, 2}}],
Rastermodtab, {{1.5, 1.5}, {3, 2}}]} ] ]
In the default case, Raster always generates an array of gray cells. As described on page 489, you
can use the option ColorFunction to apply a “coloring function” to all the cells.
You can also use the graphics primitive RasterArray. While Raster takes an array of values,
RasterArray takes an array of Mathematica graphics directives. The directives associated with each
cell are taken to determine the color of that cell. Typically the directives are chosen from the set
GrayLevel, RGBColor or Hue. By using RGBColor and Hue directives, you can create color rasters using RasterArray.
Web sample page from The Mathematica Book, Second Edition, by Stephen Wolfram, published by Addison-Wesley Publishing Company (hardcover ISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact Wolfram Research: [email protected];
http://www.wolfram.com/; 1-800-441-6284.
 1991 Wolfram Research, Inc.
Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction, or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.