How to Find the Midpoint Between Two 3D Points With Java

104 19
    • 1). Open the the .java file in which you wish to do the calculation using your preferred text editor.

    • 2). Declare six doubles inside an appropriate method of the .java file, to represent the x, y and z coordinates of the of the two points. You can use a statement such as "double x1 = 0.0;", without the quotation marks. Do this not just for x1, but also for y1, z1, x2, y2 and z2. Instead of 0.0, put the actual coordinates of the two 3d points you have. If you already have these points as variables in your program, you can skip this step.

    • 3). Declare three doubles to hold the coordinates of the midpoint you're about to calculate. To do this, type "double x3, y3, z3;" on a new line, or use whatever variable names you prefer.

    • 4). Type "x3 = (x1 + x2)/2" on a new line. This expression calculates the x coordinate of the midpoint. If you have different names for your coordinate variables, make sure you put them in instead.

    • 5). Use the same expression in two more lines for y and z: "y3 = (y1 + y2)/2;" and "z3 = (z1 + z2)/2;". You now have the x, y and z coordinates for a point midway between your two 3D points.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.