Here's an example (with source) you can use
http://www.codeproject.com
Main Topics
Browse All TopicsIf I want to draw cube on screen with silverlight, how can i do this drawing lines.
I know how to draw a line but the 2D-3d transformation I forget and I cant find a similar question I asked earlier. The user inputs the side length.
MY question is more about the mathematics as I know how to draw lines in silverlight.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here's an example (with source) you can use
http://www.codeproject.com
say, you want to project the 3D points to a 2D plane a*x+b*y+c*z+d=0
first you need to choose a view point, (x0,y0,z0),
then a 3D point (x,y,z) is project to
(r*(x-x0)+x0, r*(y-y0)+y0,r*(z-z0)+z0), where r=(ax0+by0+cz0+d)/((a(x0-x
please note that this projection does not consider any other effects.
you can choose the plane to be z=0, then r*(z-z0)+z0 would always be 0 and the 2D point is
(r*(x-x0)+x0, r*(y-y0)+y0)
sorry about that, ax0 is just a*x0 and so as by0, cz0, and a(x0-x) ..., where i dropped "*" for simplicity.
if the screen plane is z=0, which basically means a=b=d=0 and c=1
i just derived those by hands, and haven't tested them thoroughly. but at least all points on z axis are projected to (0,0) from view point (0,0,5), which seems correct.
If your camera is at (0, 0, 5) and positive z axis points out of the screen, simply map
(x, y, z) |-> (x/(5-z), y/(5-z) )
Note that it does not make sense to map any points with z>=5 (they are not in front of the camera).
In addition, you <i>may</i> want to scale in order to enlarge the picture and translate in order to move the origin (x=y=0) towrads the screen center.
The formula above is for single points.
If you want to map line segments, map the endpoints and join them - at least if both are in front of the camera; if only one of the points is in front of the camera, find the 3D point on the line that is on the camera plane (z=5) or for simplicity just a trifle in front (z=4.99, say), map it and join the two points found.
Example:
a) Consider the 3D line with end points (7, 3, -10) and (-10, 0, 1)
The first point maps to (7/15, 3/15) = (0.4667, 0.2), the second maps to (-10/4, 0/4) = (-2.5, 0).
Draw the 2D line from (0.46667, 0.2) to (-2.5, 0)
b) Consider the 3D line with end points (2,2, -5) and (3, 1, 15).
The first point maps to (2/10, 2/10) = (0.2, 0.2), but the second point is "behind" the camera.
The general point on the line is (2+t, 2-t, -5+20t). We have z=5 iff t=0.5.
WIth t=0.49, we are slightly before the camera, namely at (2.49, 1.51, 4.8); that point maps to (2.49/0.2, 1.51/0.2) = (12.45, 7.55).
It is assumed that this point is outside the viewport so that the line from (0.2, 0.2) to (12.45, 7.55) is clipped.
If (12.45, 7.55) is <i>not</i> outside the viewport, the value t=0.49 was not close enough to 0.5.
Mathematically, you should consider the limit for t->0.5 and obtain an infinitely distant point that gives a direction.
The correct -- limit -- method would suggest that you draw a line from (0.2, 0.2) in direction (2.5, 1.5) until you hit the clipping boundary.
However, for your first experiments, trying some t that leads to z sufficiently close to (but below) 5 should suffice.
Business Accounts
Answer for Membership
by: thehagmanPosted on 2009-03-05 at 08:24:37ID: 23807147
Let your cube have 3D vertices (-1,-1,-1), (-1,-1,+1), (-1,+1,-1), ..., (+1,+1,+1).
projecting a point to 2D is simple:
Either map (x,y,z) to (x,y) -- that's a parallel projection.
Or map (x,y,z) to (x/z, y/z) -- that involves perspectivity.
However, you should apply a few transformations to get interesting images: Rotate, translate, scale.
Especially for the perspective variant make sure that all z values are positive ("in front of the camera").
Maybe try this as something to play with:
1) start with the vertices as given above.
2) Rotate them around the y-axis by some amount and translate a bit into positive z direction, i.e. map (x,y,z) to
(x*cos(a) + z*sin(a), y, z*cos(a) - x*sin(a) +b)
You can choose a beween 0 and 2pi arbitrarily and b shozuld be at least about 1.5 -- e.g. try b = 10
3) Map each new (x,y,z) to (x/z, y/z) -- now we aer already in 2D
4) scale to make the cube bigger, e.g. replace (x,y) by (100x, 100y)
5) translate to the center of yourwindow or viewport or whatever
Now you have 8 points in 2D that correspond to the eight veitices of the cube.
Draw lines between them accoringly.