draw shapes with 3d points java

StdDraw3D Tutorial


StdDraw3D is a Java library that makes it like shooting fish in a barrel to create three-dimensional models, simulations, and games. This tutorial covers basic features—it is aimed at a beginner, with no feel in computer graphics. Hither is the complete StdDraw3D reference transmission.

This tutorial is oriented toward those familiar with this library's two-dimensional counterpart StdDraw. StdDraw3D strives to friction match the syntax of StdDraw, but it has a much larger API and many more features.

Installing StdDraw3D.

To ensure that you have the necessary environs for StdDraw3D, run our automatic Java installer for your operating system [ Mac OS 10 · Windows ]. Nosotros exercise not recommend a manual installation because there are numerous dependencies. Since StdDraw3D is under development so we recommend downloading the latest version of stdlib.jar unless you just ran our automatic installer. For Windows, overwrite the version in

C:\Users\username\introcs\stdlib.jar

For Mac Os X, overwrite the version in

~/Library/Extensions/stdlib.jar

To examination that StdDraw3D is installed correctly, type the following from the command line:

% java StdDraw3D

You should come across rotating text within two red concentric circles. StdDraw3D is graphics intensive, and may be slow on older computers.

Hello 3D.

Compile and run the plan Hello3D.coffee.

public class Hello3D {     public static void master(String[] args) {          // set the scale of the drawing window         StdDraw3D.setScale(-ane,i);          // draw a sphere of radius 1 centered at (0,0,0)         StdDraw3D.sphere(0, 0, 0, ane);          // render to the cartoon window         StdDraw3D.finished();     } }            

The first command sets the viewable coordinates of the drawing window to between -1 and i in all directions. The 2nd command draws a sphere at 3-dimensional coordinates (0, 0, 0) with a radius of 1. The third control says that yous are done drawing (more on this later). It's that easy—if you run this program, you will see a white sphere in the cartoon window.

If you practice not, troubleshoot using the Q & A below.

Basic commands.

StdDraw3D supports a wide range of interactive navigation controls and photographic camera modes. Try some basic navigation in the drawing window:

  • Left-click and elevate to orbit around the sphere.
  • Right-click and elevate to pan the view.
  • Gyre the mouse bicycle (or alt-click and drag) to zoom.

StdDraw3D has a black background by default, rather than the white of StdDraw. This is because 3D shading looks much nicer with a black background. You can change the groundwork color the same way equally in StdDraw. For example, the command

StdDraw3D.clear(StdDraw3D.BLUE)

sets the groundwork color to blue before cartoon. In that location are also commands for setting background images and 3D background panoramas.

There are many types of 3D shapes available for drawing in StdDraw3D. Play around with these bones functions—they all take double values as arguments.

  • StdDraw3D.sphere(10, y, z, radius);
  • StdDraw3D.cube(x, y, z, radius);
  • StdDraw3D.box(x, y, z, width, peak, depth);
  • StdDraw3D.cylinder(x, y, z, radius, height);
  • StdDraw3D.cone(x, y, z, radius, height);
  • StdDraw3D.ellipsoid(x, y, z, width, height, depth);

All of your shapes and then far have been white (the default pen color), but yous can change colors with the control

StdDraw3D.setPenColor()

.

  • StdDraw3D.setPenColor(StdDraw3D.Greenish); // Predefined green colour
  • StdDraw3D.setPenColor(StdDraw3D.Red, int blastoff); // Red with transparency (0-255)
  • StdDraw3D.setPenColor(int r, int g, int b); // RGB values (0-255)
  • StdDraw3D.setPenColor(Color col); // Color object

The default coordinate calibration for the drawing window is from 0 to 1 in all directions. You can ready the scale with the control

StdDraw3D.setScale()

. For instance,

StdDraw3D.setScale(-100, 100)

sets the 10-, y-, and z-scale of the window such that you can comfortably view a sphere of radius 100. Information technology is important to define a adept scale for your project, considering many parameters depend on it. We apply the calibration (-ane, +1) for nearly of our example programs.

BlueCone.java demonstrates some of these features. It draws a blue cone of radius 0.5 and meridian 1 at the location (0, 0, 0), on an orange background.

The camera.

And then far, the example shapes take been at the location (0, 0, 0), which is the origin of the 3D space you are working with. The three values correspond to the x-, y-, and z-coordinates along the standard Cartesian axes. By drawing shapes in unlike locations, you volition observe that by default positive x is to the correct, positive y is upwards, and positive z is out of the screen.

In the upper left corner of the drawing window, you will run into some text that looks like this:

Position: ( 0.000, 0.000, ii.414 ) Rotation: ( 0,000, 0.000, 0.000 ) Photographic camera: ORBIT_MODE

Imagine that the view you run across in the cartoon window is the result of a picture show taken past a camera. This photographic camera is an abstraction used to control the viewpoint of your projection. The listed numbers are the position and rotation of this camera.

The camera position is specified by (ten, y, z) coordinates. The rotation is specified past 3 Euler angles (xAngle, yAngle, zAngle), which are essentially successive angles of rotation about the x-, y-, and z-axes.

When you lot move effectually with the interactive controls, you lot are changing the position and rotation of the camera. You can proceeds a amend understanding by looking at how the numbers modify as you play around with the controls.

By default, the camera is centered on the origin and zoomed out on the z-axis enough to have a proper view of the scale you defined. It is easy to change the photographic camera backdrop.

The easiest way to set the camera position is with the command StdDraw3D.setCameraPosition(10, y, z). It sets the photographic camera position to the iii coordinates y'all specify. To ready the rotation of the photographic camera, utilize the command StdDraw3D.setCameraOrientation(xAngle, yAngle, zAngle). One useful play a trick on is to find a camera bending you similar using the interactive controls, and then plug in the displayed numbers into these functions to programatically gear up that viewpoint.

CameraTest.java is a simple case of setting a custom camera viewpoint. It draws half dozen simple shapes and sets the camera properties.

The photographic camera has numerous avant-garde features, including multiple modes, animation capabilities, and object-oriented control. For example, setting the camera to First-Person Mode will allow you to utilise the WASD keys and mouse, as in about estimator games. 3D rotations are a very complicated topic, and there are many options included in StdDraw3D. However, what you lot take simply learned is sufficient for about common applications.

Animation.

So far, we have discussed drawing a single scene and showing it by calling

StdDraw3D.finished()

. StdDraw3D likewise suppports animations and and user interactions.

If you lot have used StdDraw, yous are familiar with the StdDraw.clear() and StdDraw.testify()—StdDraw3D takes a similar approach for producing simple animations.

  • The function StdDraw3D.evidence(int milliseconds) displays everything you accept drawn to the drawing window, and pauses for a given number of milliseconds.
  • The office StdDraw3D.clear() erases everything in the drawing window upon the adjacent call of StdDraw3D.show(). Clearing with a color argument sets the background colour.
  • The function StdDraw3D.finished() is equivalent to calling StdDraw3D.show() an space number of times. The reason it exists is that because StdDraw3D has interactive controls—we always desire the program to be in an space loop and never exit. If the program were to leave, then the interactive features would no longer be responsive.

To create animations, you need a loop that uses the pattern of immigration the screen, drawing something, and then showing for a specified amount of time. The standard frame rate of videos is approximately 30 milliseconds per frame.

AnimationTest.java demonstrates the process described to a higher place by animating a red sphere to move in a circumvolve.

A more powerful and efficient method of blitheness is available in StdDraw3D, simply that is deferred for the avant-garde tutorial.

Wireframe shapes.

All of the shape functions you lot have seen so far have wireframe equivalents—just add the prefix

wire

. For example, call

StdDraw3D.wireSphere(0, 0, 0, i)

instead of

StdDraw3D.sphere(0, 0, 0, ane)

. WireSphere.java draws a wireframe sphere.

Rotated Shapes.

You know how to prepare the position of drawn shapes, but yous can as well specify their three dimensional rotation. Just like the camera has three Euler angles to specify it's successive rotation almost the ten, y, and z axes, so does each shape.

Every shape you have seen so far has overloaded cartoon functions that you tin can utilise to specify the 3 rotation angles. For example, use StdDraw3D.cube(x, y, z, radius, xAngle, yAngle, zAngle) to draw a rotated cube. RotationTest.java draws a cone inside a wireframe sphere, and animates them rotating in different directions.

Points, lines, and surfaces.

In add-on to 3D solids, you can draw points, lines, and surfaces in 3D space.

  • StdDraw3D.indicate(x, y, z) draws a point at the coordinates (x, y, z).
  • StdDraw3D.line(x1, y1, z1, x2, y2, z2) draws a line from (x1, y1, z1) to (x2, y2, z2).
  • StdDraw3D.polygon(double[] x, double[] y, double[] z) takes arrays of vertices as input and draws their containing surface.
  • StdDraw3D.tube(x1, y1, z1, x2, y2, z2, radius) draws a 3D cylindrical tube betwixt the points (x1, y1, z1) and (x2, y2, z2).

In add-on, the functions

StdDraw3D.points()

,

StdDraw3D.lines()

, and

StdDraw3D.triangles()

take arrays of points, lines, and mesh surfaces. These methods are much more than efficient than individually cartoon a big number of individual shapes. Hilbert3D.java draws the post-obit epitome:

3D text. The control StdDraw3D.text3D(x, y, z, String text) creates 3D text shapes from a given string. For case, Text3D.java produces the following image:

2D overlays.

You can draw second overlays on acme of the 3D drawing window, to deed as an HUD or present informational text. All functions of StdDraw are available nether the prefix of

StdDraw3D.overlay

. For case, to draw an overlaid circumvolve of radius 0.5, use

StdDraw3D.overlayCircle(0, 0, 0.5)

. Overlays do follow the scale set by

StdDraw3D.setScale()

only do non depend on the camera position or rotation. OverlayText.java provides an example.

The avant-garde tutorial.

This tutorial covers the basics of StdDraw3D. For interactive animations, large-calibration simulations, and games, we recommend the avant-garde tutorial (under evolution). The avant-garde tutorial reintroduces StdDraw3D under these premises:

  • Every Shape is an object that you can move and rotate without having to redraw.
  • The Photographic camera is an object that behaves just similar a Shape.
  • Coordinates can exist controlled using a Vector3D class rather than scalars.
  • Y'all do non need to redraw or articulate anything to create animations.

The advanced tutorial also covers advanced features, including the following:

  • Support for easy custom keyboard and mouse controls.
  • Command of lighting, colored lighting, blithe lighting.
  • Importing 3D models as shapes and animating them.
  • Combining Shapes to create cascading kinematic transformations.
  • Camera modes and projection modes.
  • Like shooting fish in a barrel sound furnishings.
  • Cosmos of custom 3D shapes.

The StdDraw3D reference manual documents and explains every method in StdDraw3D.

Q + A

Q. Whom should I contact if I find a bug or take a question?

A. This tutorial and StdDraw3D were written by Hayk Martirosyan. Experience complimentary to electronic mail him at hmartiro@princeton.edu.

Q. Where tin can I find obj model files?

A. TurboSquid has many free obj models. Annotation that many of these OBJ files won't show their materials properly because the file format is variable and the Coffee loader is not very good - they may need tweaking of the "observe and replace" way for some commands. Summary: For one color models, everything will piece of work. For models with custom materials and textures, it may take some work.

Q. Why aren't at that place more questions and answers?

A. This Q + A department is under development.

klineskillart.blogspot.com

Source: https://introcs.cs.princeton.edu/java/stddraw3d/

0 Response to "draw shapes with 3d points java"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel