Jump to content

  • Log In with Google      Sign In   
  • Create Account


Display variable information in label in C#


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 shawnre   Members   -  Reputation: 266

Like
0Likes
Like

Posted 17 December 2006 - 03:58 AM

I created a simple form in Windows to display values of my variables as different inputs were detected. Label1 = Rotation angle Label2 = X-Coordinate Label3 = Y-Coordinate I used labels as illustrated above to display the values of the variables. The problem I see is in my rotation angle. As it steps from 0, right now in .03 increments, I expect it to return back to zero. Sometimes that happens, sometimes it doesnt. In other words, if I decrement by .03, then add .03, I expect to be back at zero. Sometimes yes, sometimes no. I am guessing the the conversion from a float to a string is causing the problem, so, question is how do i display my variable values then without having to convert to string?

Sponsor:

#2 mr_101   Members   -  Reputation: 795

Like
0Likes
Like

Posted 17 December 2006 - 04:44 AM

When you convert the float into a string are you using the roundtrip option? If not, try using:


// x != y (well, it *might*)
float x = .03f;
float y = float.Parse(x.ToString());

// x == y
float x = .03f;
float y = float.Parse(x.ToString("R"));




If you don't use the "R" option with ToString() you get additional digits which is probably whats messing up the arithmetic.

#3 shawnre   Members   -  Reputation: 266

Like
0Likes
Like

Posted 17 December 2006 - 05:00 AM

LOL, sorry man, not sure what you mean. I'm such a noob, lmao.

Anyhow, this is what I have so far:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
label1.Text = Convert.ToString(rotation_angle);
label2.Text = Convert.ToString(x_coordinate);
label3.Text = Convert.ToString(y_coordinate);
}

float rotation_angle= 0;
int x_coordinate = 100;
int y_coordinate = 100;

public void Updatelabels()
{
label1.Text = Convert.ToString(rotation_angle);
label2.Text = Convert.ToString(x_coordinate);
label3.Text = Convert.ToString(y_coordinate);

}

private void button1_Click(object sender, EventArgs e)
{
y_coordinate += 1;
Updatelabels();

}

private void button3_Click(object sender, EventArgs e)
{
y_coordinate -= 1;
Updatelabels();
}

private void button2_Click(object sender, EventArgs e)
{
rotation_angle -= .03f;
Updatelabels();
}

private void button4_Click(object sender, EventArgs e)
{
rotation_angle += .03f;
Updatelabels();
}

}
}




#4 MACSkeptic   Members   -  Reputation: 122

Like
0Likes
Like

Posted 17 December 2006 - 06:21 AM

I guess maybe instead of doing the conversions this way:


label1.Text = Convert.ToString(rotation_angle);


you can try just:


label1.Text = rotation_angle.ToString("R");


everything in c# has a ToString method, don't need to use a Converter or something like that to make the cast.
This way you can use the round up idea previously put by ontheheap

Give it a shot, see if works

#5 shawnre   Members   -  Reputation: 266

Like
0Likes
Like

Posted 17 December 2006 - 06:46 AM

Quote:
Original post by MACSkeptic
I guess maybe instead of doing the conversions this way:


label1.Text = Convert.ToString(rotation_angle);


you can try just:


label1.Text = rotation_angle.ToString("R");


everything in c# has a ToString method, don't need to use a Converter or something like that to make the cast.
This way you can use the round up idea previously put by ontheheap

Give it a shot, see if works


Done, still same sort of problem. Reach + or - .12 and trying to go back to zero does not work. Would it be safe to assume that it is this conversion from float to text that is causing the error? I just dont see how stepping backwards from say .12 ends up giving me -3.7252903E-09. Granted this is close to zero, but, not where we started despite going 4 up and 4 down in increments of .03

Assuming that it is just the "cast" as it was referred to, with these 3 variables is it now possible to determine x and y coordinates based on the rotationangle? That is the heart of the matter that this "simple" program was supposed to help me with. Evidentally I need to use Sin and Cos to attempt this. Assuming a 0 rotation is pointing straight right, and the user rotates, how to adjust x and y based off the rotation?

#6 mr_101   Members   -  Reputation: 795

Like
0Likes
Like

Posted 17 December 2006 - 06:48 AM

Quote:
Original post by shawnre
LOL, sorry man, not sure what you mean. I'm such a noob, lmao.

Anyhow, this is what I have so far:

*** Source Snippet Removed ***


Ah, OK. I was thinking you were *getting* the values from the labels, not just setting them. I think your problem comes from the inaccuracy of float. Try using the Decimal type instead. Keep in mind that Decimal is larger than float or double and operations are slower. Information on floating point numbers in .NET can be found here

#7 shawnre   Members   -  Reputation: 266

Like
0Likes
Like

Posted 17 December 2006 - 06:59 AM

Speed is not really an issue in this context. However, your comment has brought up another question in addition to ones added by way of edit above your latest comment ontheheap.

I believe I read that rotation angles are done in radians (Correct?)
If so, can the variable rotation_angle still be decimal as you suggest?

Maybe I should read your link first, but, I jumped the gun.

*Edit* - Nice link, and yes, more than likely it was the float, tried with double and worked just fine.

#8 shawnre   Members   -  Reputation: 266

Like
0Likes
Like

Posted 17 December 2006 - 07:18 AM

And to correct what I just said, it is sin and cos that work in radians, in case anyone reads this, dont want to confuse anyone.

Now, based off the revised code, I am assuming that calculating the x and y coordinates can now be done using the rotation_angle.

Does the positive rotation_angle move clockwise or counter?


[Edited by - shawnre on December 17, 2006 1:18:59 PM]

#9 mr_101   Members   -  Reputation: 795

Like
0Likes
Like

Posted 17 December 2006 - 08:55 AM

I believe the positive rotation angle would move counterclockwise. See if this helps:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
public partial class Form1 : Form
{
decimal rotation_angle;
decimal x_location;
decimal y_location;

public Form1()
{
InitializeComponent();
}

public void updateFields()
{
angleTxtBox.Text = rotation_angle.ToString();
xLocTxtBox.Text = x_location.ToString();
yLocTxtBox.Text = y_location.ToString();
}

public void updateShip()
{
shipLbl.Location = new Point((int)x_location, (int)y_location);
}

private void Form1_Load(object sender, EventArgs e)
{
rotation_angle = 0.0M;
x_location = 100;
y_location = 100;

updateFields();
}

private void rotateLeftBtn_Click(object sender, EventArgs e)
{
rotation_angle = rotation_angle + .03M;
updateFields();
}

private void rotateRightBtn_Click(object sender, EventArgs e)
{
rotation_angle = rotation_angle - .03M;
updateFields();
}

private void forwardBtn_Click(object sender, EventArgs e)
{
x_location -= (decimal)(Math.Sin((double)rotation_angle));
y_location -= (decimal)(Math.Cos((double)rotation_angle));
updateShip();
}

private void backwardBtn_Click(object sender, EventArgs e)
{
x_location += (decimal)(Math.Sin((double)rotation_angle));
y_location += (decimal)(Math.Cos((double)rotation_angle));
updateShip();
}
}
}



#10 shawnre   Members   -  Reputation: 266

Like
0Likes
Like

Posted 17 December 2006 - 10:00 AM

Hey hey thanx ontheheap, exactly what I was trying to do. Didnt mean for you to do it though, but, pretty much answered all my questions. Thanks for your help, though I do have one more question.

rotation_angle = rotation_angle + .03M;

What exactly is the purpose of the M after .03??

#11 mr_101   Members   -  Reputation: 795

Like
0Likes
Like

Posted 17 December 2006 - 10:42 AM

Check out the C# Language Specification (Section 9.4.4.3).

Quote:

If no real-type-suffix is specified, the type of the real literal is double. Otherwise, the real-type-suffix determines the type of the real literal, as follows:

  • A real literal suffixed by F or f is of type float. [Example: The literals 1f, 1.5f, 1e10f, and 123.456F are all of type float. end example]

  • A real literal suffixed by D or d is of type double. [Example: The literals 1d, 1.5d, 1e10d, and 123.456D are all of type double. end example]

  • A real literal suffixed by M or m is of type decimal.







Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS