• Advertisement
Sign in to follow this  

Display variable information in label in C#

This topic is 4050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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();
}

}
}


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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();
}
}
}

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement