Hey guys,
How would I get this;
[csharp]
public static boolean isValid(double s1, double s2, double s3)
{
if((s1 < (s2 + s3)) && (s1 < (s2 + s3)) && (s1 < (s2 + s3)))
return true;
else
return false;
}[/csharp]
To give me the boolean value of 'isValid', so I can use it in this;
[csharp]
public static double area(double s1, double s2, double s3)
{
boolean valid = isValid();
if (isValid = true)
{
double area = ((0.5)* )
}
}
[/csharp]
Would I need to make an object or what?
[QUOTE=Sickle;35407811]Hey guys,
How would I get this;
[csharp]
public static boolean isValid(double s1, double s2, double s3)
{
if((s1 < (s2 + s3)) && (s1 < (s2 + s3)) && (s1 < (s2 + s3)))
return true;
else
return false;
}[/csharp]
To give me the boolean value of 'isValid', so I can use it in this;
[csharp]
public static double area(double s1, double s2, double s3)
{
boolean valid = isValid();
if (isValid = true)
{
double area = ((0.5)* )
}
}
[/csharp]
Would I need to make an object or what?[/QUOTE]
The function should work as it is written. The only issues with your code I can see at a glance are the way you're checking for the return value and the fact you're not passing the arguments to your isValid function.
[csharp]
public static double area(double s1, double s2, double s3)
{
// Your current code:
// boolean valid = isValid();
// To work correctly, this should say:
boolean valid = isValid(s1, s2, s3);
// Then, we can check this return value by doing
if(valid == true)
{
/*
We can't use
if (isValid = true)
because isValid is the name of a function, not a variable.
Furthermore, you're using the assignment operator here, a single equals sign.
This will make "isValid" have the value true, which is incorrect logic.
Instead, you need to use the comparison operator, represented by two equals signs. This checks the value of your variable.
*/
// ...
}
[/csharp]
Using Python 2.4.3, I'm trying to recursively generate the first 20 Catalan Numbers for university but this code:
[code]def cata(n):
if n == 0 or n == 1:
return 1
else:
return n + ((cata(i)*(cata(n-i))
print(cata(20))
[/code]
Is giving this error:
[code]SyntaxError: invalid syntax
[40057635@socweb8 ~]$ python Question2.py
File "Question2.py", line 7
print(cata(20))
^
[/code]
Is this something to do with the function itself being so violently wrong that it's unprintable? Anything I've found about that error has been to do with broken Python 3 installs.
[QUOTE=arachnidsGrip;35409594]Using Python 2.4.3, I'm trying to recursively generate the first 20 Catalan Numbers for university but this code:
[code]def cata(n):
if n == 0 or n == 1:
return 1
else:
return n + ((cata(i)*(cata(n-i))
print(cata(20))
[/code]
Is giving this error:
[code]SyntaxError: invalid syntax
[40057635@socweb8 ~]$ python Question2.py
File "Question2.py", line 7
print(cata(20))
^
[/code]
Is this something to do with the function itself being so violently wrong that it's unprintable? Anything I've found about that error has been to do with broken Python 3 installs.[/QUOTE]
In Python 3, "print" is a function. Prior to this, such as in version 2.4.3 as you are using, "print" is a language statement.
This basically means drop the brackets around print and you should be good to go.
[code]
print cata(20)
[/code]
Note: Apart from this issue, you'll only be generating the 20th Catalan number. You have one print statement, which you're calling for the 20th Catalan number. To print the catalan numbers from one to twenty, you'll need to use a loop around your print statement.
[editline]3rd April 2012[/editline]
You've also fucked up the number of open and close parentheses on your "else" return line.
I'd done that before, but switched back and forth trying to get it to work. Seems that it was the parantheses on the else line causing the problem, because I'm blind. Thanks.
An hour later:
[code]def cata(n):
if n == 0 or n == 1:
return 1
elif n >= 2:
y = 0
for i in range(0, n):
y == (y+cata(i)*cata(n-i))
print y
print cata(20)[/code]
Leads to a nice infinite loop. What a mess...
[QUOTE=arachnidsGrip;35409650]I'd done that before, but switched back and forth trying to get it to work. Seems that it was the parantheses on the else line causing the problem, because I'm blind. Thanks.
An hour later:
[code]def cata(n):
if n == 0 or n == 1:
return 1
elif n >= 2:
y = 0
for i in range(0, n):
y == (y+cata(i)*cata(n-i))
print y
print cata(20)[/code]
Leads to a nice infinite loop. What a mess...[/QUOTE]
Don't put the loop inside your cata function. If "cata(20)" returns the 20th Catalan number, then "cata(19)" must return the 19th, and "cata(18)" the 18th and so on.
So you want to put the loop around your print statement only. Something like
for each i in the set of 1 to 20
print cata(i)
[editline]3rd April 2012[/editline]
This, along with your previous code and my prior comments should get you the code you need to give you the output you want.
Decided to port my mandelbrot code from Love2D to Processing and it's much faster using OpenGL for drawing but I get this error that messes with my drawing:
[img]http://dl.dropbox.com/u/44918480/halpme.png[/img]
[csharp]
List<int> integerList = new List<int>();
string numberString;
int lowerLimit = 0;
int upperLimit = -1;
char numberToLookFor = '0';
Console.Write("Choose a number between 1 and 9 to look for: ");
numberToLookFor = Convert.ToChar(Console.ReadLine());
while (upperLimit < lowerLimit)
{
Console.Clear();
Console.Write("Lower limit: ");
lowerLimit = Convert.ToInt32(Console.ReadLine());
Console.Write(Environment.NewLine + "Upper limit: ");
upperLimit = Convert.ToInt32(Console.ReadLine());
}
Console.Clear();
for (int i = lowerLimit; i < upperLimit; i++)
{
numberString = Convert.ToString(i);
if (numberString.IndexOf(numberToLookFor) != -1)
{
integerList.Add(i);
Console.WriteLine(numberString);
}
}
int delta = upperLimit - lowerLimit;
float percent = ((integerList.Count/delta) * 100);
//Console.WriteLine("There are {0} instances of the number {1} between {2} and {3}", integerList.Count, numberToLookFor, lowerLimit, upperLimit);
Console.Write(Environment.NewLine + "There are ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(integerList.Count);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" instances of the number ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(numberToLookFor);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" between ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(lowerLimit);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" and ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(upperLimit);
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("." + Environment.NewLine);
Console.Write("That's {0}%.", Math.Round(percent));
Console.ReadLine();
[/csharp]
The float [I]percent[/I] is almost always 0, why the hell is that? I just can't figure this one out by myself.
Try this:
[cpp]
float percent = (float(integerList.Count/delta) * 100.f);
[/cpp]
I'm not certain on that one but it is because integerList.Count and delta are both integers so the compiler will round it down to 0 unless you specifically say you want a float.
If that doesn't work:
[cpp]
float percent = (float(integerList.Count)/float(delta) * 100.f);
[/cpp]
That should do.
I have to use GLUT for an assignment, so please don't tell me "stop using GLUT" as a solution to this problem. Believe me, I would love to but I just can't. I'm already stretching the rules by using freeglut. :v:
Anyway, my issue is as follows: I want to delete a texture at the end of my program's execution. Of course, this can only be done while I have a valid OpenGL context. Unfortunately, GLUT deletes the context at the end of glutMainLoop so I have absolutely no way of deleting my textures in time. Does anyone know how I can detect when the window is closing so I can quickly free up my textures and so on before the context is deleted?
[QUOTE=Chris220;35413670]I have to use GLUT for an assignment, so please don't tell me "stop using GLUT" as a solution to this problem. Believe me, I would love to but I just can't. I'm already stretching the rules by using freeglut. :v:
Anyway, my issue is as follows: I want to delete a texture at the end of my program's execution. Of course, this can only be done while I have a valid OpenGL context. Unfortunately, GLUT deletes the context at the end of glutMainLoop so I have absolutely no way of deleting my textures in time. Does anyone know how I can detect when the window is closing so I can quickly free up my textures and so on before the context is deleted?[/QUOTE]
Chris, 4 y u delete me on steam :(
Also cant you have a variabe in there that gets activated(turn it to true) when the window is closing and then just have an if(closing){ free stuff; }
[QUOTE=conman420;35413662]Try this:
[cpp]
float percent = (float(integerList.Count/delta) * 100.f);
[/cpp]
I'm not certain on that one but it is because integerList.Count and delta are both integers so the compiler will round it down to 0 unless you specifically say you want a float.
If that doesn't work:
[cpp]
float percent = (float(integerList.Count)/float(delta) * 100.f);
[/cpp]
That should do.[/QUOTE]
Tried those, didn't work, but this turned out to be right:
[csharp]
float percent = ((float)(integerList.Count)/(float)(delta) * 100f);[/csharp]
[QUOTE=Richy19;35414278]Chris, 4 y u delete me on steam :([/QUOTE]
Nothing personal, I was just going through and deleting everyone who I haven't actually spoken to in months. :v:
I have a bit of a clean-steam-friends-list fetish, I guess.
[QUOTE=Richy19;35414278]Also cant you have a variabe in there that gets activated(turn it to true) when the window is closing and then just have an if(closing){ free stuff; }[/QUOTE]
Believe me, I'd love to. Problem is, GLUT provides no 'warning' of the window being closed, I see nothing that I can use to catch the moment the user closes the application.
I think I'm going to just edit a glutShutdownFunc() into freeglut and recompile it. :v:
I'm thinking of picking up a few books to help boost my programming knowledge at home.
I'm thinking of getting some of these books:
[url]http://www.amazon.com/dp/0672326965/[/url]
[url]http://www.amazon.com/dp/0596528124/[/url]
[url]http://www.amazon.com/dp/0201756080/[/url]
[url]http://www.amazon.com/gp/product/0201604612/[/url]
[url]http://www.amazon.com/dp/020161622X/[/url]
[url]http://www.amazon.com/dp/0735619670/[/url]
And possibly this:
[url]http://www.udemy.com/learn-c-the-hard-way/[/url]
Any recommendations from the selection of books here or any not listed?
I already have K&R C.
People read books?
[QUOTE=ROBO_DONUT;35417985]People read books?[/QUOTE]
Yeah and I prefer to learn from a book if it's related to programming, I don't know why but I just find it easier.
This is more of a conceptual question really but I can't think of the best way to implement Box2D into my game, right now I am defining Box2D bodies like this:
[cpp]
void Player::Spawn()
{
float width = 20;
float height = 25;
GetRenderer()->SetTexture(std::string("ship.png"), true);
GetRenderer()->SetSize(Vector2(width * 3.4f,width * 3.4f));
GetRenderer()->SetDrawOrder(3);
//This is bad whats the best way to hide all this?
b2PolygonShape* shape = new b2PolygonShape;
b2Vec2 verts[3];
verts[0] = b2Vec2(-width,-height);
verts[1] = b2Vec2(width,-height);
verts[2] = b2Vec2(0, height);
shape->Set(verts, 3);
AddPhysicsShape(shape);
GetPhysObj()->SetAngularDamping(7);
GetPhysObj()->SetLinearDamping(0);
}
[/cpp]
But this is really bad as everything is all in Box2D vectors and I don't really want to expose the inner workings of Box2D to my entities. I think what I am looking for is some kind of interface (right word?), has anyone coded one for something like Box2D to implement it into their game? It would be nice to see an example I am coding blind here!
[QUOTE=mechanarchy;35409583]The function should work as it is written. The only issues with your code I can see at a glance are the way you're checking for the return value and the fact you're not passing the arguments to your isValid function.
[csharp]
public static double area(double s1, double s2, double s3)
{
// Your current code:
// boolean valid = isValid();
// To work correctly, this should say:
boolean valid = isValid(s1, s2, s3);
// Then, we can check this return value by doing
if(valid == true)
{
/*
We can't use
if (isValid = true)
because isValid is the name of a function, not a variable.
Furthermore, you're using the assignment operator here, a single equals sign.
This will make "isValid" have the value true, which is incorrect logic.
Instead, you need to use the comparison operator, represented by two equals signs. This checks the value of your variable.
*/
// ...
}
[/csharp][/QUOTE]
Oh! I see!
Thanks, man! That really helped a lot.
[QUOTE=Doritos_Man;35418082]Yeah and I prefer to learn from a book if it's related to programming, I don't know why but I just find it easier.[/QUOTE]
Well I kind of agree. I prefer to have things on paper when I read.
Though, I read a lot of 3D graphics related papers (well, I read the abstract and look at the pretty pictures/graphs, skipping everything else) online and I'd probably kill a forest or seven if I printed them all out.
I've never really had the patience for complete books beyond simple references. I've only read one textbook cover-to-cover, and it was a short one.
Nevermind, found it.
[QUOTE=Doritos_Man;35418082]Yeah and I prefer to learn from a book if it's related to programming, I don't know why but I just find it easier.[/QUOTE]
Same here, but I can't justify spending that much money on something that's going to be outdated (or worse: deprecated) in a few years when there's the continually updated resource of the internet around.
Is there some kind of method or whatever that inserts a certain number of spaces into a string? I thought I remember doing this in java or maybe I just imagined it.
I thought it was something simple like doing: "Hello" + Space(10) + "World"
And it would add 10 spaces in between.
[QUOTE=Larikang;35419482]Same here, but I can't justify spending that much money on something that's going to be outdated (or worse: deprecated) in a few years.[/QUOTE]
It's general C stuff which has been around for quite a while now and isn't changing as much.
Also does anyone recommend those or a book on Algorithms?
Hey guys, why am I getting an error when I try to get isValid in my other class?
I'm taking it from;
[csharp]
public class MyTriangle
{
double s1 = 5;
double s2 = 6;
double s3 = 7;
public static boolean isValid(double s1, double s2, double s3)
{
if((s1 < (s2 + s3)) && (s1 < (s2 + s3)) && (s1 < (s2 + s3)))
return true;
else
return false;
}
public static double area(double s1, double s2, double s3)
{
boolean valid = isValid(s1, s2, s3);
if (valid == true)
{
double s = (s1 + s2 + s3)/2;
double num = s*((s-s1)*(s-s2)*(s-s3));
double area = Math.sqrt(num);
}
else
{
System.out.println("Invalid triangle!");
}
return area(s1,s2,s3);
}
}[/csharp]
And I'm trying to give isValid and area to this class, and print out the values;
[csharp]
public class MyTriangleTest
{
public static void main(String[] args)
{
MyTriangle triangleObject = new MyTriangle();
triangleObject.isValid(0, 0, 0);
triangleObject.area(0, 0, 0);
System.out.println(isValid);
System.out.println("The area of the triangle is " +area);
}
}
[/csharp]
What am I doing wrong?
[QUOTE=Sickle;35419733]Hey guys, why am I getting an error when I try to get isValid in my other class?
I'm taking it from;
[csharp]
public class MyTriangle
{
double s1 = 5;
double s2 = 6;
double s3 = 7;
public static boolean isValid(double s1, double s2, double s3)
{
if((s1 < (s2 + s3)) && (s1 < (s2 + s3)) && (s1 < (s2 + s3)))
return true;
else
return false;
}
public static double area(double s1, double s2, double s3)
{
boolean valid = isValid(s1, s2, s3);
if (valid == true)
{
double s = (s1 + s2 + s3)/2;
double num = s*((s-s1)*(s-s2)*(s-s3));
double area = Math.sqrt(num);
}
else
{
System.out.println("Invalid triangle!");
}
return area(s1,s2,s3);
}
}[/csharp]
And I'm trying to give isValid and area to this class, and print out the values;
[csharp]
public class MyTriangleTest
{
public static void main(String[] args)
{
MyTriangle triangleObject = new MyTriangle();
triangleObject.isValid(0, 0, 0);
triangleObject.area(0, 0, 0);
System.out.println(isValid);
System.out.println("The area of the triangle is " +area);
}
}
[/csharp]
What am I doing wrong?[/QUOTE]
Your first problem is that you have inadvertently made the instance function "area" in the MyTriangle class recursive. When you return "area(s1,s2,s3);" instead of just "area", you are just recursively recalling the same function with the same input, thus causing an infinite loop. I'm not sure how Java handles tail calls, but it may or may not cause a stack overflow at some point.
Once you have changed it to return the local "area" variable instead, you need to assign the returned values of your two calls in the main function of MyTriangleTest to some variable or else the return is optimized out completely.
I.E.
[csharp]
bool isValid = triangleObject.isValid(0, 0, 0);
double area = triangleObject.area(0, 0, 0);
[/csharp]
Okay, I've gone and gotten myself stuck again.
I'm working on 3D using DirectX for rendering and GLM for math-calculations. Here's my problem:
Objects show up with inverted normals. I've checked the data being uploaded and the normals are right, and the winding order appears right. However, post vertex-shader something fishy happens.
B) When I move the camera up (as printed by it's position), everything in my scene moves up instead of down.
Here's the code I'm using for my VIEW matrix:
[cpp]
CTempCamera::CTempCamera()
{
m_vecPosition = glm::vec3(10, 0, 0);
m_vecForward = glm::vec3(0, 0, 1);
m_vecRight = glm::vec3(1, 0, 0);
m_vecUp = glm::vec3(0, 1, 0);
}
void CTempCamera::Update(CUserCMD cmd)
{
float moveRate = 1.0f;
//Set Position
if(cmd.buttonBits & IN_FORWARD)
m_vecPosition += (m_vecForward * moveRate);
if(cmd.buttonBits & IN_BACKWARD)
m_vecPosition -= (m_vecForward * moveRate);
if(cmd.buttonBits & IN_MOVERIGHT)
m_vecPosition -= (m_vecRight * moveRate);
if(cmd.buttonBits & IN_MOVELEFT)
m_vecPosition += (m_vecRight * moveRate);
if(cmd.buttonBits & IN_LEANRIGHT)
m_vecPosition += (m_vecUp * moveRate);
if(cmd.buttonBits & IN_LEANLEFT)
m_vecPosition -= (m_vecUp * moveRate);
if(cmd.buttonBits != 0)
Message("Pos: %f %f %f", m_vecPosition.x, m_vecPosition.y, m_vecPosition.z);
//Update Angles
m_vecForward = glm::normalize(m_vecForward);
//Make UP vector a right-angle to Right and Forward
m_vecUp = glm::cross(m_vecForward, m_vecRight);
m_vecUp = glm::normalize(m_vecUp);
//Make RIGHT vector a right-angle to Up and Forward
m_vecRight = glm::cross(m_vecUp, m_vecForward);
m_vecRight = glm::normalize(m_vecRight);
//Generate a view matrix
glm::mat4 view = glm::mat4(1.0f);
//This doesn't take the up vector or rotation into account, what should I be doing?
view = glm::translate(view, m_vecPosition);
g_pEngineInterface->SetPlayerCamera(view);
}
[/cpp]
Here is the SetPlayerCamera(...) function:
[cpp]
void CRenderMain::CreateViewPerspMatrix( glm::mat4 viewMatrix )
{
//I know I don't need to re-calculate perspective very frame, but I wanted to get this working first.
float aspectRatio = g_pVideo->GetWidth() / g_pVideo->GetHeight();
m_projMat = glm::perspective(45.0f, aspectRatio, 0.1f, 100.0f);
m_viewMat = viewMatrix;
}
[/cpp]
And finally here is the MVP as it's being uploaded to the GPU:
[cpp]
//MVP
glm::mat4 worldMat = glm::mat4(1.0f);
glm::mat4 MVP = m_projMat * m_viewMat * worldMat;
MVP = glm::transpose(MVP);
//Upload MVP to GPU
g_pDeviceContext->UpdateSubresource(m_pMVPBuffer, 0, NULL, &MVP, 0, 0);
g_pDeviceContext->VSSetConstantBuffers(0, 1, &m_pMVPBuffer);
[/cpp]
Can anyone sort me out so that it follows the left-hand coordinate system?
[b]Edit:[/b]
I'm leaving this for the future's sake:
To solve my issue with my camera moving the wrong way and things like that, I wrote my own view matrix. I picked it up off of [url=http://www.toymaker.info/Games/html/camera.html]here[/url] and plugged in my own numbers, except I did not negate the m_vecPosition.z. [b]Edit:[/b] This still does not work, rotating and then moving forward doesn't do the right thing.
[cpp]
void CTempCamera::Update(CUserCMD cmd)
{
float moveRate = 1.0f;
//Set Position
if(cmd.buttonBits & IN_FORWARD)
m_vecPosition += (m_vecForward * moveRate);
if(cmd.buttonBits & IN_BACKWARD)
m_vecPosition -= (m_vecForward * moveRate);
if(cmd.buttonBits & IN_MOVERIGHT)
m_vecPosition += (m_vecRight * moveRate);
if(cmd.buttonBits & IN_MOVELEFT)
m_vecPosition -= (m_vecRight * moveRate);
if(cmd.buttonBits & IN_LEANRIGHT)
m_vecRight += (m_vecUp * moveRate);
if(cmd.buttonBits & IN_LEANLEFT)
m_vecPosition -= (m_vecUp * moveRate);
if(cmd.buttonBits != 0)
Message("Pos: %f %f %f", m_vecPosition.x, m_vecPosition.y, m_vecPosition.z);
//Update Angles
m_vecForward = glm::normalize(m_vecForward);
//Make UP vector a right-angle to Right and Forward
m_vecUp = glm::cross(m_vecForward, m_vecRight);
m_vecUp = glm::normalize(m_vecUp);
//Make RIGHT vector a right-angle to Up and Forward
m_vecRight = glm::cross(m_vecUp, m_vecForward);
m_vecRight = glm::normalize(m_vecRight);
//Generate a View matrix
glm::mat4 view = glm::mat4(1.0f);
view[0][0] = m_vecRight.x;
view[0][1] = m_vecUp.x;
view[0][2] = m_vecForward.x;
view[0][3] = 0.0f;
view[1][0] = m_vecRight.y;
view[1][1] = m_vecUp.y;
view[1][2] = m_vecForward.y;
view[1][3] = 0.0f;
view[2][0] = m_vecRight.z;
view[2][1] = m_vecUp.z;
view[2][2] = m_vecForward.z;
view[2][3] = 0.0f;
view[3][0] = -m_vecPosition.x;
view[3][1] = -m_vecPosition.y;
view[3][2] = m_vecPosition.z;
view[3][3] = 1.0f;
g_pEngineInterface->SetPlayerCamera(view);
}
[/cpp]
However, this still leaves object normals being inverted. Here is a screenshot from PIX, which shows the location of a vertex before and after the vertex shader. Here is a picture showing the problem:
[img]http://img195.imageshack.us/img195/658/ss20120403192705.png[/img]
And here is the HLSL code that powers it:
[code]
cbuffer ProjViewWorldMatrix : register( b0 )
{
matrix gProjViewWorld;
}
struct VS_INPUT
{
float3 Position : POSITION;
float3 Normal : NORMAL;
float2 TexCoord : TEXCOORD;
float4 Bones : BONES;
float4 Weights : WEIGHTS;
};
struct PS_INPUT
{
float3 Normal : NORMAL;
float4 Position : SV_POSITION;
float2 TexCoord : TEXCOORD0;
};
PS_INPUT VS(VS_INPUT input)
{
PS_INPUT output = (PS_INPUT) 0;
output.Position = mul(float4(input.Position, 1), gProjViewWorld);
output.TexCoord = input.TexCoord;
output.Normal = input.Normal;
return output;
}
float4 PS(PS_INPUT input) : SV_TARGET
{
float4 baseDiffuse = float4(1.0f, 0.5f, 0.2f, 1.0f);
baseDiffuse = mul(baseDiffuse, float4(input.Normal, 0));
return baseDiffuse;
}
[/code]
(I'm aware the diffuse-color equation is all sorts of wrong, I just needed something to stop the shaded-ness).
Here is a picture of PIX showing where it goes wrong:
[img]http://img843.imageshack.us/img843/7502/ss20120403193016.png[/img]
It looks kind of like the Z is getting inverted?
[QUOTE=Rayjingstorm;35419881]Your first problem is that you have inadvertently made the instance function "area" in the MyTriangle class recursive. When you return "area(s1,s2,s3);" instead of just "area", you are just recursively recalling the same function with the same input, thus causing an infinite loop. I'm not sure how Java handles tail calls, but it may or may not cause a stack overflow at some point.
Once you have changed it to return the local "area" variable instead, you need to assign the returned values of your two calls in the main function of MyTriangleTest to some variable or else the return is optimized out completely.
I.E.
[csharp]
bool isValid = triangleObject.isValid(0, 0, 0);
double area = triangleObject.area(0, 0, 0);
[/csharp][/QUOTE]
The problem is that when I change it to 'return area' it gives me an unresolved variable error.
[QUOTE=Sickle;35420732]The problem is that when I change it to 'return area' it gives me an unresolved variable error.[/QUOTE]
Because the definition of area is within an if statement, there is a case where no value is returned. You need to initialize and give "area" some value, probably negative to indicate that the triangle is impossible. You might consider declaring it before the if, and then simply giving it a negative value when you print out that the triangle is invalid.
[QUOTE=Rayjingstorm;35420849]Because the definition of area is within an if statement, there is a case where no value is returned. You need to initialize and give "area" some value, probably negative to indicate that the triangle is impossible. You might consider declaring it before the if, and then simply giving it a negative value when you print out that the triangle is invalid.[/QUOTE]
Oooohh.
[csharp]
public static double area(double s1, double s2, double s3)
{
boolean valid = isValid(s1, s2, s3);
double area = 0;
if (valid == true)
{
double s = (s1 + s2 + s3)/2;
double num = s*((s-s1)*(s-s2)*(s-s3));
double area = Math.sqrt(num);
}
else
{
System.out.println("Invalid triangle!");
}
return area;
}
[/csharp]
So I initialized it, gave it a value of 0, that worked, and then it gave me a 'Duplicate area local variable area'
And also, would it really be necessary to give it a negative value in the else statement if it isn't even printing it?
Sorry, you need to Log In to post a reply to this thread.