Symbol not found, declared array in parameter - java

This method takes in an array of planets and calculates their net x force component on the planet called upon. I get symbol not found error on compile for a1
public double calcForceExertedByX(Planet[] a1){
double forceX;
for (int i = 0; i < a1.length(); i++){
forceX = forceX + 6.667e-11 *(mass*a1[i].mass)/( xxPos - a1[i].xxPos)*(xxPos - a1[i].xxPos);
}
return forceX;
}
below is the complete code, as requested.
public class Planet {
public double xxPos;
public double yyPos;
public double xxVel;
public double yyVel;
public double mass;
public String imgFileName;
public Planet(double xPs, double yPs, double xVl, double
yVl, double m, String imge){
xxPos = xPs;
yyPos = yPs;
xxVel = xVl;
yyVel = yVl;
mass = m;
imgFileName = imge;
}
public Planet(Planet p){
xxPos = p.xxPos;
yyPos = p.yyPos;
xxVel = p.xxVel;
yyVel = p.yyVel;
mass = p.mass;
imgFileName = p.imgFileName;
}
public double calcDistance(Planet p1){
double distx = xxPos - p1.xxPos;
double disty = yyPos - p1.yyPos;
double dist = Math.sqrt(distx*distx + disty*disty);
return dist;
}
public double calcForceExertedBy(Planet p1){
double force = 6.667e-11 *(mass*p1.mass)/(this.calcDistance(p1)*this.calcDistance(p1));
return force;
}
public double calcForceExertedByX(Planet[] a1){
double forceX;
for (int i = 0; i < a1.length(); i++){
forceX = forceX + 6.667e-11 *(mass*a1[i].mass)/( xxPos - a1[i].xxPos)*(xxPos - a1[i].xxPos);
}
return forceX;
}
public double calcForceExertedByY(Planet[] a1){
double forceY;
for (int i = 0; i <a1.length(); i++){
forceY = forceY + 6.667e-11 *(mass*a1[i].mass)/( yyPos - a1[i].yyPos)*(yyPos - a1[i].yyPos);
}
return forceY;
}
}
Windows 7 JAVAc 11.01

I am guessing the error that is trying to be referenced is that Planet[] a1 does not have method length(). Arrays use .length rather than a method call.
Also forceX is not initialised.

Related

stdDraw draw over background java

I am doing this project that simulates the solar system using java's stdDraw. I want to make the change where I can show the trace of the corn, because my ultimate goal is to make this corn fly in a heart shape and if I can draw out the trace of the corn I can present the heart. Right now I've been trying to do it, but it seems like the background image is blocking the trace. And if I comment out the background image, all of the planets will show their trace. I don't know what to do, help!!!
Here is the Object Class:
public class BodyExtreme{
public double xxPos;
public double yyPos;
public double xxVel;
public double yyVel;
public double mass;
public String imgFileName;
private static final double G = 6.67e-11;
public BodyExtreme(double xP, double yP, double xV, double yV, double m, String img){
xxPos = xP;
yyPos = yP;
xxVel = xV;
yyVel = yV;
mass = m;
imgFileName = img;
}
public BodyExtreme(BodyExtreme b){
xxPos = b.xxPos;
yyPos = b.yyPos;
xxVel = b.xxVel;
yyVel = b.yyVel;
mass = b.mass;
imgFileName = b.imgFileName;
}
public double calcDistance(BodyExtreme b) {
double dx = b.xxPos - this.xxPos;
double dy = b.yyPos - this.yyPos;
return Math.sqrt(dx * dx + dy * dy);
}
public double calcForceExertedBy(BodyExtreme b) {
if (this.calcDistance(b) == 0) {
return 0;
} else {
return (G * b.mass * this.mass)/(this.calcDistance(b) * this.calcDistance(b));
}
}
public double calcForceExertedByX(BodyExtreme b) {
return (this.calcForceExertedBy(b) * (b.xxPos - this.xxPos) / this.calcDistance(b));
}
public double calcForceExertedByY(BodyExtreme b) {
return (this.calcForceExertedBy(b) * (b.yyPos - this.yyPos) / this.calcDistance(b));
}
public double calcNetForceExertedByX(BodyExtreme[] b) {
int i = 0;
double sum = 0;
while (i < b.length) {
if (this.equals(b[i])) {
sum += 0;
i += 1;
} else {
sum = sum + this.calcForceExertedByX(b[i]);
i += 1;
}
} return sum;
}
public double calcNetForceExertedByY(BodyExtreme[] b) {
int i = 0;
double sum = 0;
while (i < b.length) {
if (this.equals(b[i])) {
sum += 0;
i += 1;
} else {
sum = sum + this.calcForceExertedByY(b[i]);
i += 1;
}
} return sum;
}
public void update(double dt, double fX, double fY) {
double ax = fX / this.mass;
double ay = fY / this.mass;
double vx = this.xxVel + dt * ax;
double vy = this.yyVel + dt * ay;
double px = this.xxPos + dt * vx;
double py = this.yyPos + dt * vy;
this.xxPos = px;
this.yyPos = py;
this.xxVel = vx;
this.yyVel = vy;
}
public void draw() {
StdDraw.picture(this.xxPos, this.yyPos, "images/" + this.imgFileName);
}
public void lonelyplanet_update1(){
this.xxPos = this.xxPos + 45000000;
this.yyPos = this.yyPos + 100000000;
this.draw();
}
}
Here is the Main method class:
import java.util.Scanner;
public class NBodyExtreme{
public static double readRadius(String name) {
In in = new In(name);
int NumPlanets = in.readInt();
double Size = in.readDouble();
return Size;
}
public static BodyExtreme[] readBodies(String name) {
In in = new In(name);
int NumPlanets = in.readInt();
double Size = in.readDouble();
BodyExtreme[] bodies = new BodyExtreme[NumPlanets];
int i = 0;
while (i < NumPlanets) {
bodies[i] = new BodyExtreme(in.readDouble(), in.readDouble(), in.readDouble(), in.readDouble(), in.readDouble(), in.readString());
i += 1;
}
return bodies;
}
public static void main(String[] args) {
double T = Double.parseDouble(args[0]); /** Stoping Time */
double dt = Double.parseDouble(args[1]); /** Time Step */
String filename = args[2];
BodyExtreme[] bodies = readBodies(filename); /** Array of Bodies */
double radius = readRadius(filename); /** Canvas Radius */
In in = new In(filename);
int NumPlanets = in.readInt(); /** Number of Planets */
String imageToDraw = "images/starfield.jpg"; /** Background */
StdDraw.enableDoubleBuffering();
StdDraw.setScale(-2*radius, 2*radius);
StdDraw.clear();
StdDraw.picture(0, 0, imageToDraw); /** Draw Initial Background */
StdDraw.show();
int k = 0;
while (k < NumPlanets-1) { /** Draw Planets */
bodies[k].draw();
k += 1;
}
StdDraw.enableDoubleBuffering();
double time = 0.0;
while (time < T) {
double[] xForces = new double[NumPlanets-1];
double[] yForces = new double[NumPlanets-1];
int i = 0;
while (i < NumPlanets-1) {
xForces[i] = bodies[i].calcNetForceExertedByX(bodies);
yForces[i] = bodies[i].calcNetForceExertedByY(bodies);
i += 1;
}
i = 0;
while (i < NumPlanets-1) {
bodies[i].update(dt, xForces[i], yForces[i]);
i += 1;
}
bodies[NumPlanets-1].lonelyplanet_update1();
bodies[NumPlanets-1].draw();
StdDraw.show();
StdDraw.picture(0, 0, imageToDraw);
int j = 0;
while (j < NumPlanets) {
bodies[j].draw();
j += 1;
}
StdDraw.show();
StdDraw.pause(10);
}
time += dt;
}
}
This is what happens when the trace of route is blocked with the background image:
This is what happens when the trace is not blocked, but I only want the corn's trace not to be blokcekd.
you have a flaw in your animation logic:
to animate your screen you have to
update the model (each element, eg. backgroud or bodY),
then draw each model by using StdDraw.picture(..) and
finally make that content visible (by using StdDraw.show();)
for each time step in your animation (while (time < T){..}) you have to do all three steps.
public static void main(String[] args) {
...
StdDraw.enableDoubleBuffering(); //it's enough to call this only once!
double time = 0.0;
while (time < T) {
//do all the update stuff first, as shown in your code above
//then draw ONCE the BackGround:
StdDraw.picture(0, 0, imageToDraw);
//then draw all the planets (yes, i missed one)
while (j < NumPlanets) {
bodies[j].draw();
j += 1;
}
//finally make all the previous drawing visible
StdDraw.show();
//wait a bit for the next animation step
StdDraw.pause(10);
time += dt;
}
}

Im wondering why when I get the string output at then end perimeter is not being calculated and put in the string

I'm trying to do my first methods.
I'm having trouble getting the perimeter to display the output as a String. I'm wondering why this is happening. I very well could have other problems inside of my code but the perimeter not outputting is what is holding me back right now.
Following is my code.
public class Polygon {
public Polygon() {
int numSides = 4;
double SideLength = 5.0;
double xCoord = 0.0;
double yCoord = 0.0;
double apothem = 5.0;
double perimeter = 20.0;
}
private int numSides = 2;
private double SideLength = 2;
private double xCoord;
private double yCoord;
private double apothem;
private double perimeter;
private double area;
public Polygon(int numsides, double sideLength, double xcoord, double ycoord, double Apothem, double Perimeter) {
SideLength = sideLength;
numSides = numsides;
xCoord = xcoord;
yCoord = ycoord;
apothem = Apothem;
perimeter = Perimeter;
}
public int getnumsides() {
return numSides;
}
public double getSideLength() {
return SideLength;
}
public double getxcoord() {
return xCoord;
}
public double getycoord() {
return yCoord;
}
public double getApothem() {
return apothem;
}
public double getPerimeter() {
return numSides * SideLength;
}
public void setsideLength(double ssideLength){
SideLength = ssideLength;
}
public void setnumsides(int snumsides){
numSides = snumsides;
}
public void setxcoord(double sxcoord){
xCoord = sxcoord;
}
public void setycoord(int sycoord){
yCoord = sycoord;
}
public void setApothem(int sApothem){
apothem = sApothem;
}
public void setPerimeter(int sPerimeter){
perimeter = sPerimeter;
}
public String toString() {
String str = numSides + " is the number of sides the polygon has and " + SideLength + " is how long the sides are. "+ xCoord + " is how long the x coordinate is and " + yCoord + " is how long the y coordinate is. " + apothem + " is the apothem of the polygon and " + perimeter + " is the perimeter of the polygon.";
return str;
}
public void getArea() {
area = .5 * apothem * perimeter;
}
}
You are again defining same field variables in Polygon() constructor which is not required because you have already defined them as private class members. This is the reason that some values are setting as default while printing toString() method.
Your Polygon() constructor should be look like this:
public Polygon() {
numSides = 4;
SideLength = 5.0;
xCoord = 0.0;
yCoord = 0.0;
apothem = 5.0;
perimeter = 20.0;
}
What do you mean by "perimeter not outputting"?
May be this is what you want to achieve?
public static void main(String[] args){
Polygon p = new Polygon();
double perimeter = p.getPerimeter();
System.out.println("Perimeter is " + perimeter);
}

I'm trying to draw a new dot every 250 milliseconds but it only draws a dot once

//I'm trying to draw a new dot every 250 milliseconds but it only draws the dot a single time. I have tried fixing it many times, but it still will only paint a single dot, rather than one after 250 milliseconds. Is this a problem with the timer or the paint method? Here is the code:
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Window extends JPanel{
private int size;
private static double maxValue;
private double elevation;
private double vertV;
public double horizV;
public double gravity;
public double range;
public double time;
public double t = 0;
public Window(int s, double v, double e, double v2, double g,double h,double r,double t){
size = s;
maxValue = v;
elevation = e;
vertV = v2;
gravity = g;
horizV = h;
range = r;
time = t;
setPreferredSize(new Dimension(size, size));
}
public void paintComponent(Graphics g){
g.drawLine(size/25, 0,size/25, size);
g.drawLine(0, size - (size/25), size, size - (size/25));
double[] lines = getLine();
int x = size/5 + (size/25), y = size - (size/25);
int x2 = x;
for(int i = 0; i < 4; i++){
g.drawLine(x, y+5, x, y-5);
g.drawString(lines[i]+"",x-size/50,y+size/30);
x+=x2;
}
int yx = size/25, yy = size - (size/5 + (size/25));
int y2 = size/5 + (size/25);
for(int i=0;i<4;i++){
g.drawLine(yx-5, yy, yx+5, yy);
g.drawString(lines[i]+"",yx-size/25,yy+size/30);
yy -= y2;
}
drawDots(g);
}
//this is the place where i make the dots but it only makes one.
//used to be a for loop but i altered it to an if statement so i could paint one dot at a time
public void drawDots(Graphics g)
{
double ratio = (size-((size/25)*2))/maxValue;
double fx;
double xvalue;
// This for loop is where dots are drawn, each iteration draws one dot. It starts at zero, and counts up to the time variable t.
if(t<=time)
{
t+=0.025;
t = Math.round(t*1000.0)/1000.0;
fx = function(t);
xvalue = xfunction(t);
if(fx >= 0){
System.out.print("Time: " + t + " " + "Range: " + xvalue + " " + "Height: ");
System.out.println(fx);
g.drawLine((int)(size/25+(ratio*xvalue)), (int)((size-(size/25))-(ratio*fx)),
(int)(size/25+(ratio*xvalue)), (int)((size-(size/25))-(ratio*fx)));
}
}
}
//where i make the timer
//250 mill
public void dostuff()
{
int delay = 250;
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
repaint();
}
};
new Timer(delay, taskPerformer).start();
}
public double xfunction(double t){
double x = 0.0;
x = Math.round(horizV * t * 1000.0)/1000.0;
return x;
}
public double function(double t){
double fx = 0.0;
fx = Math.round((vertV*t + .5*(-(gravity))*(t*t) + elevation)*1000.0)/1000.0;
return fx;
}
private static double[] getLine(){
double increment = maxValue / 4;
double currentLine = 0;
double[] lines = new double[4];
for(int i = 0; i < 4; i++){
currentLine+=increment;
lines[i] = Math.round(currentLine * 10.0)/10.0;
}
return lines;
}
}
This is the original version of the code that displays the projectile's motion, but it does not wait 250 milliseconds between drawing each point:
import javax.swing.JPanel;
import java.awt.*;
public class Window extends JPanel{
private int size;
private static double maxValue;
private double elevation;
private double vertV;
public double horizV;
public double gravity;
public double range;
public double time;
public Window(int s, double v, double e, double v2, double g,double h,double r,double t){
size = s;
maxValue = v;
elevation = e;
vertV = v2;
gravity = g;
horizV = h;
range = r;
time = t;
setPreferredSize(new Dimension(size, size));
}
public void paintComponent(Graphics g){
g.drawLine(size/25, 0,size/25, size);
g.drawLine(0, size - (size/25), size, size - (size/25));
double[] lines = getLine();
int x = size/5 + (size/25), y = size - (size/25);
int x2 = x;
for(int i = 0; i < 4; i++){
g.drawLine(x, y+5, x, y-5);
g.drawString(lines[i]+"",x-size/50,y+size/30);
x+=x2;
}
int yx = size/25, yy = size - (size/5 + (size/25));
int y2 = size/5 + (size/25);
for(int i=0;i<4;i++){
g.drawLine(yx-5, yy, yx+5, yy);
g.drawString(lines[i]+"",yx-size/25,yy+size/30);
yy -= y2;
}
drawDots(g);
}
public void drawDots(Graphics g){
double ratio = (size-((size/25)*2))/maxValue;
double fx;
double xvalue;
// This for loop is where dots are drawn, each iteration draws one dot. It starts at zero, and counts up to the time variable t.
for(double t=0;t<=time; t+=0.025){
t = Math.round(t*1000.0)/1000.0;
fx = function(t);
xvalue = xfunction(t);
if(fx >= 0){
System.out.print("Time: " + t + " " + "Range: " + xvalue + " " + "Height: ");
System.out.println(fx);
g.drawLine((int)(size/25+(ratio*xvalue)), (int)((size-(size/25))-(ratio*fx)),
(int)(size/25+(ratio*xvalue)), (int)((size-(size/25))-(ratio*fx)));
}
}
}
public double xfunction(double t){
double x = 0.0;
x = Math.round(horizV * t * 1000.0)/1000.0;
return x;
}
public double function(double t){
double fx = 0.0;
fx = Math.round((vertV*t + .5*(-(gravity))*(t*t) + elevation)*1000.0)/1000.0;
return fx;
}
private static double[] getLine(){
double increment = maxValue / 4;
double currentLine = 0;
double[] lines = new double[4];
for(int i = 0; i < 4; i++){
currentLine+=increment;
lines[i] = Math.round(currentLine * 10.0)/10.0;
}
return lines;
}
}

Variable is not passing by

I am trying to pass aF variable. But when debugging, it shows to have a value of 0. Any idea? below is the code I am using (Update: I included the whole code).
import java.util.ArrayList;
import java.util.List;
public class EOS {
//defining constants, input variables
public static final double GAS_CONSTANT = 8.3144598; //J K-1 mol-1
double criticalTemperature;
double criticalPressure;
double temperature;
double pressure;
double molecularWeight;
public EOS(double criticalTemperature, double criticalPressure, double temperature, double pressure, double molecularWeight) {
this.criticalTemperature = criticalTemperature;
this.criticalPressure = criticalPressure;
this.temperature = temperature;
this.pressure = pressure;
this.molecularWeight = molecularWeight;
}
// calculation of A* and B* (values of "a" and "b" will be provided by subclasses)
public double aStar(double a) {
return a * pressure / (Math.pow(GAS_CONSTANT, 2) * Math.pow(temperature, 2));
}
public double bStar(double b) {
return b * pressure / (GAS_CONSTANT * temperature);
}
//calculation of Z Value. The idea is to form the cubic function of Z as follow:
public List<Double> calculateZ(double aStar, double bStar, double uValue, double wValue) {
List<Double> solution = new ArrayList<>();
double a, b, c, q, r, d;
a = -1 - bStar + uValue * bStar;
b = aStar + wValue * Math.pow(bStar, 2) - uValue * bStar - uValue * Math.pow(bStar, 2);
c = - bStar * aStar - wValue * Math.pow(bStar, 2) - wValue * Math.pow(bStar, 3);
q = (3*b-Math.pow(a, 2))/3;
r = (2*Math.pow(a, 3)-9*a*b+27*c)/27;
d = (Math.pow(q, 3)/27) + (Math.pow(r, 2)/4);
if (d == 0) {
double x1 = 2*Math.pow(-r/2, 1/3) -(a/3);
double x2 = -2*Math.pow(-r/2, 1/3) -(a/3);
double x3 = x2;
double[] temp = {x1, x2, x3};
for (int i = 0; i < temp.length; i++) {
if (temp[i] > 0) {
solution.add(temp[i]);
}
}
} else if (d > 0) {
double x1 = Math.pow((-r/2)+Math.pow(d, 0.5),1/3)+Math.pow((-r/2)+Math.pow(d, 0.5),1/3)-(a/3);
solution.add(x1);
} else {
double theta = Math.acos((3*r/(2*q))*Math.sqrt(-3/q));
double x1 = 2*Math.sqrt(-q/3)*Math.cos(theta/3)-(a/3);
double x2 = 2*Math.sqrt(-q/3)*Math.cos((theta+2*Math.PI)/3)-(a/3);
double x3 = 2*Math.sqrt(-q/3)*Math.cos((theta+4*Math.PI)/3)-(a/3);
double[] temp = {x1, x2, x3};
for (int i = 0; i < temp.length; i++) {
if (temp[i] > 0) {
solution.add(temp[i]);
}
}
}
return solution;
}
}
Here the subclass
import java.util.Collections;
public class Soave extends EOS {
public Soave (double aFactor, double criticalTemperature, double criticalPressure, double temperature, double pressure, double molecularWeight) {
super(criticalTemperature, criticalPressure, temperature, pressure, molecularWeight);
this.aF = aFactor;
this.fW = 0.48+1.574*aFactor-0.176*Math.pow(aFactor, 2);
}
double aF;
double uValue = 1;
double wValue = 0;
double fW;
public double reducedTemperature = temperature / criticalTemperature;
public double bValue = 0.08664*GAS_CONSTANT*criticalTemperature/criticalPressure;
public double aValue() {
double term1 = 1 - Math.sqrt(reducedTemperature);
double term2 = 1+fW*term1;
double term3 = Math.pow(term2, 2.0);
double term4 = Math.pow(GAS_CONSTANT, 2)*Math.pow(criticalTemperature, 2.0);
return 0.42748*term3*term4/criticalPressure;
}
public double aStarValue = aStar(aValue());
public double bStarValue = bStar(bValue);
public double gasZValue = Collections.max(calculateZ(aStarValue, bStarValue, uValue, wValue));
public double liquidZValue = Collections.min(calculateZ(aStarValue, bStarValue, uValue, wValue));
public double gasDensity = pressure * molecularWeight / (1000 * gasZValue * GAS_CONSTANT * temperature);
public double liquidDensity = pressure * molecularWeight / (1000 * liquidZValue * GAS_CONSTANT * temperature);
}
So now when we create an instance of Soave for the following inputs, we should get for liquidDensity a value of 568.77
double p = 500000;
double t = 318.15;
double pC = 3019900;
double tC = 507.9;
double aF = 0.299;
double mW = 86;
Soave soave = new Soave(aF, tC, pC, t, p, mW);
System.out.println(soave.liquidDensity);
You set your fW variable prior to actually setting the value of aF so it is using the default value of the primitive double which is 0.
Either create a getter for fW where you do the calculations or more the calculation for fW inside the constructor block.
So Either you do like this:
public class Soave extends EOS {
public double aF;
double uValue = 1;
double wValue = 0;
public double fW;
public Soave (double aFactor, double criticalTemperature, double criticalPressure, double temperature, double pressure, double molecularWeight) {
super(criticalTemperature, criticalPressure, temperature, pressure, molecularWeight);
this.aF = aFactor;
fW = 0.48+1.574*aF-0.176*Math.pow(aF, 2); //This will give you the proper number.
}
Alternatively add a getter and do the calculation directly(No need for the fW-variable in the class then).
public double getfWValue() {
return 0.48+1.574*aF-0.176*Math.pow(aF, 2);
}
If so then use that directly in your print-statement instead.
System.out.println(soave.getfWValue());
It is surely the matter of passing the argument or reading it. Look at the piece of code where you pass the value(Most likely you pass 0, it's quite "hard" to make it 0 while reading). If you still can't find your mistake, post the proper code here.

Color - Finding the closest color

I was following the guidelines as described here, but when my android app examines a picture taken in a well-lit room and tries to match every color to the closest basic color (black, red, blue, green, etc.), most of the colors are associated with black. I have no idea why this happens and I have spent 3 hours examining my code to find out where the flaw was. Can anyone suggest where my error is?
Here is my code:
public double getMinEValue(int color1, int color2) {
double result_value = 0;
double[] color1_values = getColorValues(color1);
double[] color2_values = getColorValues(color2);
double delta_L = color1_values[0] - color2_values[0];
double delta_C = Math.sqrt(Math.pow(color1_values[1], 2)+Math.pow(color1_values[2], 2))-Math.sqrt(Math.pow(color2_values[1], 2)+Math.pow(color2_values[2], 2));
double delta_a = color1_values[1]-color2_values[1];
double delta_b = color1_values[2]-color2_values[2];
double delta_H = Math.sqrt(Math.pow(delta_a, 2)+Math.pow(delta_b, 2)+Math.pow(delta_C, 2));
double k_1 = 0.045; double k_2 = 0.015;
double s_c = 1+k_1*Math.sqrt(Math.pow(color1_values[1], 2)+Math.pow(color1_values[2], 2));
double s_h = 1+k_2*Math.sqrt(Math.pow(color1_values[1], 2)+Math.pow(color1_values[2], 2));
result_value = Math.sqrt(Math.pow(delta_L, 2)+Math.pow((delta_C/s_c), 2)+Math.pow((delta_H/s_h), 2));
return result_value;
}
public double[] getColorValues(int color1) {
double[] return_value = new double[3];
double r = Color.red(color1)/255;
double g = Color.green(color1)/255;
double b = Color.blue(color1)/255;
double r_linear = makeLinear(r) * 100;
double g_linear = makeLinear(g) * 100;
double b_linear = makeLinear(b) * 100;
double[][] matrix = {{0.4124, 0.3576, 0.1805}, {0.2126, 0.7152, 0.0722}, {0.0193, 0.1192, 0.9508}};
double[] linear_matrix = {r_linear, g_linear, b_linear};
double[] result_matrix = new double[3];
result_matrix = multiplyMatrices(matrix, linear_matrix);
//double X_n = 109.85; double Y_n = 100.00; double Z_n = 35.58; // Illuminant A
double X_n = 95.047; double Y_n = 100.00; double Z_n = 108.883; // D65
double L_star = 116*f(result_matrix[1]/Y_n)-16;
double a_star = 500*(f(result_matrix[0]/X_n)-f(result_matrix[1]/Y_n));
double b_star = 200*(f(result_matrix[1]/Y_n)-f(result_matrix[2]/Z_n));
return_value[0] = L_star; return_value[1] = a_star; return_value[2] = b_star;
return return_value;
}
private double f(double t) {
double return_value;
if (Double.compare(t, Math.pow((6/29), 3)) > 0) {
return_value = Math.pow(t, (1/3));
} else {
return_value = (1/3)*Math.pow((29/6), 2)*t+(4/29);
}
return return_value;
}
private double makeLinear(double c) {
double return_value = 0;
if (Double.compare(0.04045, c)<=0) {
return_value = c/12.92;
} else {
double a = 0.055;
return_value = Math.pow(((c + a) / (1 + a)), 2.4);
}
return return_value;
}
private double[] multiplyMatrices(double[][] matrix, double[] other_matrix) {
double[] return_matrix = new double[3];
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
return_matrix[i] += matrix[i][j]*other_matrix[j];
}
}
return return_matrix;
}
You've got a whole lot of integer divisions that need to be floating point divisions. You need to cast one of the operands of each division to double, or include a decimal point, to get these to work. For example, you have
Color.red(color1)/255
which should be
Color.red(color1)/255.0
and you also have expressions like
(1/3)*Math.pow((29/6), 2)*t+(4/29);
which needs to be
(1.0/3)*Math.pow((29.0/6), 2)*t+(4.0/29);
and many others. You have made this same mistake several times.

Categories

Resources