So, I've got a method that returns the area of a shape defined by its points (given in CCW or CW order, it doesn't really matter). I tested it with some shapes and it seems to be working.
The problem is, I want to use this method with GPS coordinates, and I want to return the result in m² or km², but that's definitly not what happens. In fact, I don't even know in which unit the result is when I use this method with that kind of coordinates.
So the question is, how to convert the result I have into m² or km² ? I tried some things, but either it does not work or it's inaccurate.
Here's my method, if you want to check :
public static double getArea(List<Vector2D> points) {
double firstSum = 0, secondSum = 0;
for (int i = 0 ; i < points.size()-1 ; i++) {
firstSum += points.get(i).x * points.get(i+1).y;
secondSum += points.get(i).y * points.get(i+1).x;
}
firstSum += points.get( points.size()-1 ).x * points.get(0).y;
secondSum += points.get( points.size()-1 ).y * points.get(0).x;
return Math.abs((firstSum-secondSum)/2);
}
(Vector2D is the class I use for points, with x as the latitude and y as the longitude)
The problem is that you're not taking the (approximately) spherical nature of the Earth into account. For a start, you need to take into account the radius of the Earth - you could have the same list of latitude and longitude on a smaller (or bigger) planet, but the radius would be different, and consequently the area would be different too.
You can use the approach in this question. It's trivial to convert that to Java:
public static double CalculatePolygonArea(List<Vector2D> coordinates)
{
double area = 0;
if (coordinates.size() > 2)
{
for (int i = 0; i < coordinates.size()-1; i++)
{
Vector2D p1, p2;
p1 = coordinates.get(i);
p2 = coordinates.get(i + 1);
area += Math.toRadians(p2.x - p1.x) * (2 + Math.sin(Math.toRadians(p1.y))
+ Math.sin(Math.toRadians(p2.y)));
}
area = area * R * R / 2;
}
return Math.abs(area);
}
(assuming Vector2D.x is the longitude and Vector2D.y is the latitude).
R is the radius of the Earth. Use a value in the unit you want the area result to be in (e.g. 6_371_000 metres for square metres, 6_371 km for square km, 3_959 miles for square miles...)
Related
I'm scratching my head with the implementation of an algorithm I have been led to believe will calculate the coefficients to an equation that will give me an ellipse that will bound a set of points. Given that I don't know how the algorithm actually does what it is supposed to do, I'm stumped as to why the algorithm isn't actually working as I think it should. I am as sure as I can be that I have implemented the algorithm accurately. I realise that I probably have stuffed up somewhere.
My implementation was modelled from this implementation in C++ because I found it easier to work with than the pseudo code given here. The OP of the C++ implementation says that it is based on the same pseudo code.
Here is my implementation:
// double tolerance = 0.2;
// int n = 10;
// int d = 2;
double tolerance=0.02;
int n=10;
int d=2;
// MatrixXd p = MatrixXd::Random(d,n);
RealMatrix p=new BlockRealMatrix(d,n,new double[][]{{70,56,44,93,77,12,30,51,35,82,74,38,92,49,22,69,71,91,39,13}},false);
// MatrixXd q = p;
// q.conservativeResize(p.rows() + 1, p.cols());
RealMatrix q=p.createMatrix(d+1,n);
q.setSubMatrix(p.getData(),0,0);
// for(size_t i = 0; i < q.cols(); i++)
// {
// q(q.rows() - 1, i) = 1;
// }
//
// const double init_u = 1.0 / (double) n;
// MatrixXd u = MatrixXd::Constant(n, 1, init_u);
double[]ones=new double[n];
double[]uData=new double[n];
for(int i=0;i<n;i++){
ones[i]=1;
uData[i]=((double)1)/((double)n);
}
q.setRow(d,ones);
// int count = 1;
// double err = 1;
int count=0;
double err=1;
while(err>tolerance){
// MatrixXd Q_tr = q.transpose();
RealMatrix qTr=q.transpose();
// MatrixXd X = q * u.asDiagonal() * Q_tr;
RealMatrix uDiag=MatrixUtils.createRealDiagonalMatrix(uData);
RealMatrix qByuDiag=q.multiply(uDiag);
RealMatrix x=qByuDiag.multiply(qTr);
// MatrixXd M = (Q_tr * X.inverse() * q).diagonal();
RealMatrix qTrByxInverse=qTr.multiply(MatrixUtils.inverse(x));
RealMatrix qTrByxInverseByq=qTrByxInverse.multiply(q);
int r=qTrByxInverseByq.getRowDimension();
double mData[]=new double[r];
for(int i=0;i<r;i++){
mData[i]=qTrByxInverseByq.getEntry(i,i);
}
// double maximum = M.maxCoeff(&j_x, &j_y);
// As M is a matrix formed from mData where only cells on the
// diagonal are populated with values greater than zero, the row
// and column values will be identical, and will be equal to the
// place where the maximum value occurs in mData. The matrix M
// is never used again in the algorithm, and hence creation of
// the matrix M is unnecessary.
int iMax=0;
double dMax=0;
for(int i=0;i<mData.length;i++){
if(mData[i]>dMax){
dMax=mData[i];
iMax=i;
}
}
// double step_size = (maximum - d - 1) / ((d + 1) * (maximum + 1));
double stepSize=(dMax-d-1)/((d+1)*(dMax+1));
// MatrixXd new_u = (1 - step_size) * u;
double[]uDataNew=new double[n];
for(int i=0;i<n;i++){
uDataNew[i]=(((double)1)-stepSize)*uData[i];
}
// new_u(j_x, 0) += step_size;
uDataNew[iMax]+=stepSize;
// MatrixXd u_diff = new_u - u;
// for(size_t i = 0; i < u_diff.rows(); i++)
// {
// for(size_t j = 0; j < u_diff.cols(); j++)
// u_diff(i, j) *= u_diff(i, j); // Square each element of the matrix
// }
// err = sqrt(u_diff.sum());
double sum=0;
for(int i=1;i<n;i++){
double cell=uDataNew[i]-uData[i];
sum+=(cell*cell);
}
err=Math.sqrt(sum);
// count++
// u = new_u;
count++;
uData=uDataNew;
}
// MatrixXd U = u.asDiagonal();
RealMatrix uFinal=MatrixUtils.createRealDiagonalMatrix(uData);
// MatrixXd A = (1.0 / (double) d) * (p * U * p.transpose() - (p * u) * (p * u).transpose()).inverse();
// Broken down into the following 9 sub-steps:
// 1 p * u
double[][]uMatrixData=new double[1][];
uMatrixData[0]=uData;
RealMatrix u=new BlockRealMatrix(n,1,uMatrixData,false);
RealMatrix cFinal=p.multiply(u);
// 2 (p * u).transpose()
RealMatrix two=cFinal.transpose();
// 3 (p * u) * (p * u).transpose()
RealMatrix three=cFinal.multiply(two);
// 4 p * U
RealMatrix four=p.multiply(uFinal);
// 5 p * U * p.transpose()
RealMatrix five=four.multiply(p.transpose());
// 6 p * U * p.transpose() - (p * u) * (p * u).transpose()
RealMatrix six=five.subtract(three);
// 7 (p * U * p.transpose() - (p * u) * (p * u).transpose()).inverse()
RealMatrix seven=MatrixUtils.inverse(six);
// 8 1.0 / (double) d
double eight=((double)1)/d;
// 9 MatrixXd A = (1.0 / (double) d) * (p * U * p.transpose() - (p * u) * (p * u).transpose()).inverse()
RealMatrix aFinal=seven.scalarMultiply(eight);
// MatrixXd c = p * u; This has been calculated in sub-step (1) above and stored as cFinal.
System.out.println();
System.out.println("The coefficients of ellipse's equation are given as follows:");
for(int i=0;i<aFinal.getRowDimension();i++){
for(int j=0;j<aFinal.getColumnDimension();j++){
System.out.printf(" %3.8f",aFinal.getEntry(i,j));
}
System.out.println();
}
System.out.println();
System.out.println("The the axis shifts are given as follows:");
for(int i=0;i<cFinal.getRowDimension();i++){
for(int j=0;j<cFinal.getColumnDimension();j++){
System.out.printf(" %3.8f",cFinal.getEntry(i,j));
}
System.out.println();
}
// Get the centre of the set of points, which will be the centre of the
// ellipse. This part was not actually included in the C++
// implementation. I guess the OP considered it too trivial.
double xmin=0;
double xmax=0;
double ymin=0;
double ymax=0;
for(int i=0;i<p.getRowDimension();i++){
double x=p.getEntry(i,0);
double y=p.getEntry(i,1);
if(i==0){
xmin=xmax=x;
ymin=ymax=y;
}else{
if(x<xmin){
xmin=x;
}else if(x>xmax){
xmax=x;
}
if(y<ymin){
ymin=y;
}else if(y>ymax){
ymax=y;
}
}
}
double x=(xmax-xmin)/2+xmin;
double y=(ymax-ymin)/2+ymin;
System.out.println();
System.out.println("The centre of the ellipse is given as follows:");
System.out.printf(" The x axis is %3.8f.\n",x);
System.out.printf(" The y axis is %3.8f.\n",y);
System.out.println();
System.out.println("The algorithm completed ["+count+"] iterations of its while loop.");
// This code constructs and displays a yellow ellipse with a black border.
ArrayList<Integer>pointsx=new ArrayList<>();
ArrayList<Integer>pointsy=new ArrayList<>();
for (double t=0;t<2*Math.PI;t+=0.02){ // <- or different step
pointsx.add(this.getWidth()/2+(int)(cFinal.getEntry(0,0)*Math.cos(t)*aFinal.getEntry(0,0)-cFinal.getEntry(1,0)*Math.sin(t)*aFinal.getEntry(0,1)));
pointsy.add(this.getHeight()/2+(int)(cFinal.getEntry(0,0)*Math.cos(t)*aFinal.getEntry(1,0)+cFinal.getEntry(1,0)*Math.sin(t)*aFinal.getEntry(1,1)));
}
int[]xpoints=new int[pointsx.size()];
Iterator<Integer>xpit=pointsx.iterator();
for(int i=0;xpit.hasNext();i++){
xpoints[i]=xpit.next();
}
int[]ypoints=new int[pointsy.size()];
Iterator<Integer>ypit=pointsy.iterator();
for(int i=0;ypit.hasNext();i++){
ypoints[i]=ypit.next();
}
g.setColor(Color.yellow);
g.fillPolygon(xpoints,ypoints,pointsx.size());
g.setColor(Color.black);
g.drawPolygon(xpoints,ypoints,pointsx.size());
This program generates the following output:
The coefficients of ellipse's equation are given as follows:
0.00085538 0.00050693
0.00050693 0.00093474
The axis shifts are given as follows:
54.31114965
55.60647648
The centre of the ellipse is given as follows:
The x axis is 72.00000000.
The y axis is 47.00000000.
The algorithm completed [23] iterations of its while loop.
I find it somewhat strange that entries of the 2x2 matrix are very small. I am led to believe that these entries are the coefficients used to describe the orientation of the ellipse, while the second 2x1 matrix describes the size of the ellipse's x and y axes.
I understand that the equations used to obtain the points are referred to as parametric equations. They have a form that is quoted here.
The location of the centre of the ellipse and the computation of these values has been added by me. They do not appear in the C++ implementation, and after I married the output of this algorithm to the parametric equations used to describe the ellipse, I was lead to believe that the OP of the C++ implementation gave the wrong impression that this 2x1 matrix described the ellipse's centre. I admit that the impression I formed could be wrong, because if one assumes I am right, then the centre (half-way between the lowest and highest values of both axes) appears to be wrong; it is less than the size of the y axis, which I take is a radial measure.
When I plug these values into the parametric equations for the ellipse to generate the points I then use to create a Polygon, the generated shape occupies a single pixel. Considering the values given in the 2x2 matrix describing the orientation, this is what I would expect.
Hence, it seems to me that there is some problem in how I generate the 2x2 matrix that produces the orientation.
I have done my best to be concise and to provide all the relevant facts, as well as any relevant impressions I have formed whether they be right or wrong. I hope someone can provide an answer to this question.
Unfortunately, I haven't been able to find help for this question.
However, I was able to find a compromise solution involving implementations in multiple languages for an enclosing circle here. I'll leave this question for someone else to answer if a better solution can be offered.
I took a stab at implementing the Minimum Bounding Ellipse in Java today using the same psuedocode you referenced in your question. I must admit, I don't understand the math but I do know that the super small numbers that you see in the "coefficients of ellipse" matrix are normal.
For my implementation, I ported MatLab code by Nima Moshtagh. In the comments section on the MatLab page, there is some code by Peter Lawrence which looks something like this:
C=inv(C);
tq=linspace(-pi,pi,M); % for display purposes, M is the number of points on the ellipse
[Ve,De]=eig(C);
De=sqrt(diag(De));
[l1,Ie] = max(De);
veig=Ve(:,Ie);
thu=atan2(veig(2),veig(1));
l2=De(setdiff([1 2],Ie));
U=[cos(thu) -sin(thu);sin(thu) cos(thu)]*[l1*cos(tq);l2*sin(tq)];
plot(U(1,:)+m(1),U(2,:)+m(2))
C is the 2x2 matrix describing the ellipse and m is the center point.
The resultant U is an array of x,y coordinates. If you add the coordinates to the center point, you'll get a ring of points that can be used to render the ellipse.
Again, I'm not 100% sure what the math is doing but it works.
I'm writing a program that will rotate a rectangular prism around a point. It handles the rotations via 3 rotation methods that each manage a rotation around a single axis (X, Y, and Z). Here's the code
public void spinZ(Spin spin) {
if (x == 0 && y == 0) {
return;
}
double mag = Math.sqrt(x * x + y * y);
double pxr = Math.atan(y / x);
x = Math.cos(spin.zr + pxr) * mag;
y = Math.sin(spin.zr + pxr) * mag;
}
public void spinY(Spin spin) {
if (z == 0 && x == 0) {
return;
}
double mag = Math.sqrt(x * x + z * z);
double pxr = Math.atan(z / x);
x = Math.cos(spin.yr + pxr) * mag;
z = Math.sin(spin.yr + pxr) * mag;
}
public void spinX(Spin spin) {
if (z == 0 && y == 0) {
return;
}
double mag = Math.sqrt(y * y + z * z);
double pxr = Math.atan(z / y);
y = Math.cos(spin.xr + pxr) * mag;
z = Math.sin(spin.xr + pxr) * mag;
}
public void addSpin(Spin spin) {
spinY(spin);
spinX(spin);
spinZ(spin);
}
Spin is a useless class that stores three doubles (which are rotations). These methods basically convert the rotations into 2D vectors (how I store the points) and rotate them as such. The first if statement makes sure the 2D vectors don't a magnitude of 0. They are allowed to, but in that case it's not necessary to carry out the rotation calculations. The other part just handles the trig. The bottom method just ties everything together and allows me to quickly change the order of the rotations (because order should and does affect the final rotation).
The problem isn't with the individual rotations but when they all come together. I can easily get a single rotation around a single axis to work without distorting the rectangular prism. When I put them all together, like if you were to call addSpin().
When spinY is called first, the prism is distorted when the rotations include a Y rotation (if the y component of the rotation is zero, and no rotation around the y-axis should occur, then no distortion occurs). In fact, if spinY() is called anytime but last a distortion of the cube will occur.
The same is the case with spinZ(). If spinZ() is called last, the cube won't get warped. However spinX() can go anywhere and not cause a distortion.
So the question is: Is there a problem with how I'm going about the rotations? The other question is while all rotations cannot be encompassed by rotations along just the X and Y axes or any other pair of distinct axes (like X and Z, or Y and Z), can those three sets collectively make all rotations? To clarify, can the rotations, which cannot be reached by a set of rotations around the X and Y axes, be reached by a set of rotations around the X and Z axes or the Y and Z axes?
I trust the medium I'm using to display the prisms. It's a ray-tracer I made that works well with rectangular prisms. This is a more math-based question, but it has a fairly comprehensive programming component.
These are some parallel calculations that still yield in distortions.
public void spinZ(Spin spin) {
double c = Math.cos(spin.yr);
double s = Math.sin(spin.yr);
double xp = x*c - y*s;
double yp = y*s + x*c;
x = xp;
y = yp;
}
public void spinY(Spin spin) {
double c = Math.cos(spin.yr);
double s = Math.sin(spin.yr);
double zp = z*c - x*s;
double xp = z*s + x*c;
x = xp;
z = zp;
}
public void spinX(Spin spin) {
double c = Math.cos(spin.yr);
double s = Math.sin(spin.yr);
double yp = y*c - z*s;
double zp = z*c + y*s;
y = yp;
z = zp;
}
Your checks for things like
x == 0
are unnecessary and dangerous as a double almost never will have the precise value 0. The atan when you have a division can lead to catastrophic loss of precision as well.
Why are they unnecessary? Because the following performs your rotation in a cleaner (numerically stable) fashion:
double c = Math.cos(spin.yr);
double s = Math.cos(spin.yr);
double zp = z*c - x*s;
double xp = z*s + x*c;
x = xp;
z = zp;
Of course, my example assumes you treat the y rotation with a right handed orientation, but from your sample code you seem to be treating it as left handed. Anyways, the wikipedia article on the Rotation matrix explains the math.
I am trying to calculate the net acceleration due to gravity in order to build a simple space flight sim using (G*(m1 * m2) / d * d) / m1. The ship tends to go in a semi-correct direction in a stair step pattern.
The update function of the main class
public void update()
{
double[] accels = new double[bodies.size()];//acceleration of the planets
double[][] xyaccels = new double[bodies.size()][2];//storing the x and y
for(Body n: bodies){
int count = 0;
double dist = distance(ship.loc.x,ship.loc.y,n.loc.x,n.loc.y);
double acel = getAccel(n.mass, ship.mass, dist);
accels[count] = acel;
double alpha = getAngle(ship.loc.x,ship.loc.y,n.loc.x,n.loc.y);
//xyaccels[count][0] = Math.cos(alpha) * acel;
//xyaccels[count][1] = Math.sin(alpha) * acel;
//split the acceleration into the x and y
XA += Math.cos(alpha) * acel;
YA += Math.sin(alpha) * acel;
count++;
}
ship.update(XA, YA);
//XA = 0;
//YA = 0;
accels = null;
xyaccels = null;
}
update function for the spaceship
public void update(double XA, double YA){
xAccel += XA;
yAccel += YA;
//add the x-acceleration and the y-acceleration to the loc
loc.x += Math.round(xAccel);
loc.y += Math.round(yAccel);
}
You don't update location from acceleration. You don't have any equations relating velocity to acceleration that I can see. Your physics are wrong.
The 2D n body problem requires four coupled ordinary differential equations for each body. Acceleration, velocity, and displacement are all 2D vector quantities.
dv/dt = F/m // Newton's F = ma
ds/dt = v // Definition of velocity that you'll update to get position.
You have to integrate all of them together.
I'm assuming you know something about calculus and physics. If you don't, it's a better idea to find a library written by someone else who does: something like JBox2D.
I'm using JFreeChart to draw chart. I have XYSeries with points (0, 0), (1, 2), (2, 5) and I want to read Y value for let's say x=1.5.
Is it possible to read value for points which are not in XYSeries? I couldn't find similar topic.
This is not supported directly. It does not make sense in many cases: There simply is no data available for x=1.5. The value there could be 1000.0, or -3.141. You don't know.
However, you're most likely looking for a linear interpolation. The pragmatic approach is thus to find the interval that contains the respective x-value, and interpolate the y-values linearly.
There are some technical caveats. E.g. the XYSeries may be not sorted, or may contain duplicate x-values, in which case there is no unique y-value for a given x-value. But for now, we can assume that the data set does not have these properties.
The following is an example of how this could be implemented. Note that this is not very efficient. If you have to compute many intermediate values (that is, if you intend to call the interpolate method very often), it would be beneficial to create a tree-based data structure that allows looking up the interval in O(logn).
However, if this is not time critical (e.g. if you only intend to show the value in a tooltip or so), you may interpolate the values like this:
import java.util.List;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYSeries;
public class XYInterpolation
{
public static void main(String[] args)
{
XYSeries s = new XYSeries("Series");
s.add(0,0);
s.add(1,2);
s.add(2,5);
double minX = -0.5;
double maxX = 3.0;
int steps = 35;
for (int i=0; i<=steps; i++)
{
double a = (double)i / steps;
double x = minX + a * (maxX - minX);
double y = interpolate(s, x);
System.out.printf("%8.3f : %8.3f\n", x, y);
}
}
private static double interpolate(XYSeries s, double x)
{
if (x <= s.getMinX())
{
return s.getY(0).doubleValue();
}
if (x >= s.getMaxX())
{
return s.getY(s.getItemCount()-1).doubleValue();
}
List<?> items = s.getItems();
for (int i=0; i<items.size()-1; i++)
{
XYDataItem i0 = (XYDataItem) items.get(i);
XYDataItem i1 = (XYDataItem) items.get(i+1);
double x0 = i0.getXValue();
double y0 = i0.getYValue();
double x1 = i1.getXValue();
double y1 = i1.getYValue();
if (x >= x0 && x <= x1)
{
double d = x - x0;
double a = d / (x1-x0);
double y = y0 + a * (y1 - y0);
return y;
}
}
// Should never happen
return 0;
}
}
(This implementation clamps at the limits. This means that for x-values that are smaller than the minimum x-value or larger than the maximum x-value, the y-value of the minimum/maximum x-value will be returned, respectively)
You can use DatasetUtils.findYValue() from package org.jfree.data.general
I'm programming a software renderer in Java, and am trying to use Z-buffering for the depth calculation of each pixel. However, it appears to work inconsistently. For example, with the Utah teapot example model, the handle will draw perhaps half depending on how I rotate it.
My z-buffer algorithm:
for(int i = 0; i < m_triangles.size(); i++)
{
if(triangleIsBackfacing(m_triangles.get(i))) continue; //Backface culling
for(int y = minY(m_triangles.get(i)); y < maxY(m_triangles.get(i)); y++)
{
if((y + getHeight()/2 < 0) || (y + getHeight()/2 >= getHeight())) continue; //getHeight/2 and getWidth/2 is for moving the model to the centre of the screen
for(int x = minX(m_triangles.get(i)); x < maxX(m_triangles.get(i)); x++)
{
if((x + getWidth()/2 < 0) || (x + getWidth()/2 >= getWidth())) continue;
rayOrigin = new Point2D(x, y);
if(pointWithinTriangle(m_triangles.get(i), rayOrigin))
{
zDepth = zValueOfPoint(m_triangles.get(i), rayOrigin);
if(zDepth > zbuffer[x + getWidth()/2][y + getHeight()/2])
{
zbuffer[x + getWidth()/2][y + getHeight()/2] = zDepth;
colour[x + getWidth()/2][y + getHeight()/2] = m_triangles.get(i).getColour();
g2.setColor(m_triangles.get(i).getColour());
drawDot(g2, rayOrigin);
}
}
}
}
}
Method for calculating the z value of a point, given a triangle and the ray origin:
private double zValueOfPoint(Triangle triangle, Point2D rayOrigin)
{
Vector3D surfaceNormal = getNormal(triangle);
double A = surfaceNormal.x;
double B = surfaceNormal.y;
double C = surfaceNormal.z;
double d = -(A * triangle.getV1().x + B * triangle.getV1().y + C * triangle.getV1().z);
double rayZ = -(A * rayOrigin.x + B * rayOrigin.y + d) / C;
return rayZ;
}
Method for calculating if the ray origin is within a projected triangle:
private boolean pointWithinTriangle(Triangle triangle, Point2D rayOrigin)
{
Vector2D v0 = new Vector2D(triangle.getV3().projectPoint(modelViewer), triangle.getV1().projectPoint(modelViewer));
Vector2D v1 = new Vector2D(triangle.getV2().projectPoint(modelViewer), triangle.getV1().projectPoint(modelViewer));
Vector2D v2 = new Vector2D(rayOrigin, triangle.getV1().projectPoint(modelViewer));
double d00 = v0.dotProduct(v0);
double d01 = v0.dotProduct(v1);
double d02 = v0.dotProduct(v2);
double d11 = v1.dotProduct(v1);
double d12 = v1.dotProduct(v2);
double invDenom = 1.0 / (d00 * d11 - d01 * d01);
double u = (d11 * d02 - d01 * d12) * invDenom;
double v = (d00 * d12 - d01 * d02) * invDenom;
// Check if point is in triangle
if((u >= 0) && (v >= 0) && ((u + v) <= 1))
{
return true;
}
return false;
}
Method for calculating surface normal of a triangle:
private Vector3D getNormal(Triangle triangle)
{
Vector3D v1 = new Vector3D(triangle.getV1(), triangle.getV2());
Vector3D v2 = new Vector3D(triangle.getV3(), triangle.getV2());
return v1.crossProduct(v2);
}
Example of the incorrectly drawn teapot:
What am I doing wrong? I feel like it must be some small thing. Given that the triangles draw at all, I doubt it's the pointWithinTriangle method. Backface culling also appears to work correctly, so I doubt it's that. The most likely culprit to me is the zValueOfPoint method, but I don't know enough to know what's wrong with it.
My zValueOfPoint method was not working correctly. I'm unsure why :( however, I changed to a slightly different method of calculating the value of a point in a plane, found here: http://forum.devmaster.net/t/interpolation-on-a-3d-triangle-using-normals/20610/5
To make the answer here complete, we have the equation of a plane:
A * x + B * y + C * z + D = 0
Where A, B, and C are the surface normal x/y/z values, and D is -(Ax0 + By0 + Cz0).
x0, y0, and z0 are taken from one of the vertices of the triangle. x, y, and z are the coordinates of the point where the ray intersects the plane. x and y are known values (rayOrigin.x, rayOrigin.y) but z is the depth which we need to calculate. From the above equation we derive:
z = -A / C * x - B / C * y - D
Then, copied from the above link, we do:
"Note that for every step in the x-direction, z increments by -A / C, and likewise it increments by -B / C for every step in the y-direction.
So these are the gradients we're looking for to perform linear interpolation. In the plane equation (A, B, C) is the normal vector of the plane.
It can easily be computed with a cross product.
Now that we have the gradients, let's call them dz/dx (which is -A / C) and dz/dy (which is -B / C), we can easily compute z everywhere on the triangle.
We know the z value in all three vertex positions.
Let's call the one of the first vertex z0, and it's position coordinates (x0, y0). Then a generic z value of a point (x, y) can be computed as:"
z = z0 + dz/dx * (x - x0) + dz/dy * (y - y0)
This found the Z value correctly and fixed my code. The new zValueOfPoint method is:
private double zValueOfPoint(Triangle triangle, Point2D rayOrigin)
{
Vector3D surfaceNormal = getNormal(triangle);
double A = surfaceNormal.x;
double B = surfaceNormal.y;
double C = surfaceNormal.z;
double dzdx = -A / C;
double dzdy = -B / C;
double rayZ = triangle.getV1().z * modelViewer.getModelScale() + dzdx * (rayOrigin.x - triangle.getV1().projectPoint(modelViewer).x) + dzdy * (rayOrigin.y - triangle.getV1().projectPoint(modelViewer).y);
return rayZ;
}
We can optimize this by only calculating most of it once, and then adding dz/dx to get the z value for the next pixel, or dz/dy for the pixel below (with the y-axis going down). This means that we cut down on calculations per polygon significantly.
this must be really slow
so much redundant computations per iteration/pixel just to iterate its coordinates. You should compute the 3 projected vertexes and iterate between them instead look here:
triangle/convex polygon rasterization
I dislike your zValueOfPoint function
can not find any use of x,y coordinates from the main loops in it so how it can compute the Z value correctly ?
Or it just computes the average Z value per whole triangle ? or am I missing something? (not a JAVA coder myself) in anyway it seems that this is your main problem.
if you Z-value is wrongly computed then Z-Buffer can not work properly. To test that look at the depth buffer as image after rendering if it is not shaded teapot but some incoherent or constant mess instead then it is clear ...
Z buffer implementation
That looks OK
[Hints]
You have too much times terms like x + getWidth()/2 why not compute them just once to some variable? I know modern compilers should do it anyway but the code would be also more readable and shorter... at least for me