|
|
Did anyone come across any idea how to plot data in C#, without using OpenGl? I want to plot few 3D graphs.
|
|
|
|
|
Hi,
Im playing with a little pet project and am totally stumped on a bit of Schoolboy maths
I have a class called "Wall" which is essentially two Corner instances forming a line between them (surprisingly this class represents a wall). The Corner class I wont show as its basically a class which encapsulates PointF instance and implements my interface IDrawable.
Wall is defined as follows
public class Wall
{
private Corner c1;
private Corner c2;
private double angle;
private double length;
private PointF[] points;
public Wall(Corner c1,Corner c2)
{
this.c1 = c1;
this.c2 = c2;
this.angle = this.CalcAngle();
this.length = this.CalcLength();
this.GetPoints()
}
}
The problem I am having is with GetPoints (or possibly CalcAngle), as I cant seem to get the points correctly along the line (they are all drawn in my app - the wall as a line and the points as ellipses)
Below is the code for the three highlighted methods above - anyone spot my error?
private double CalcAngle()
{
float opp = c2.Position.Y-c1.Position.Y;
float adj = c2.Position.X-c1.Position.X;
double rads = Math.Atan(opp/adj);
return (rads * (180/Math.PI));
}
private double CalcLength()
{
float opp = c1.Position.Y-c2.Position.Y;
float adj = c1.Position.X-c2.Position.X;
float oppsqu = opp*opp;
float adjsqu = adj*adj;
float squ = oppsqu+adjsqu;
return Math.Sqrt(squ);
}
private void GetPoints()
{
int interval = 20;
float x = c1.Position.X;
float y = c1.Position.Y;
System.Collections.ArrayList pointList = new System.Collections.ArrayList();
for(int i=0;i<this.length;i+=interval)
{
float opp = (float)Math.Sign(DegToRad(this.angle))*interval;
float adj = (float)Math.Cos(DegToRad(this.angle))*opp;
x+= adj;
y+= opp;
PointF point = new PointF(x,y);
Console.WriteLine("\t\tPoint:{0}",point);
pointList.Add(point);
}
points = new PointF[pointList.Count];
pointList.CopyTo(points,0);
}
|
|
|
|
|
If you have two points and you want some points on that line, you just work out the ratio between the width and length, and use that to calculate how far to move in one direction when you move a known amount in the other. No trig is needed.
For example, if the points are 0,0 and 20, 10, then you have a ratio of 2:1, so if you want to calculate the X value when Y is 5, you multiply 5 by 2/1, which is obviously 2. And so on.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Thankks Cristian, I had kind of worked this out however what stumped me is that I wanted the points at specific intervals on the line, so I was thinking of this as the hypotenuse and trying to work out the opposite and adjacent lengths to apply to the X and Y.
So if I wanted the points at intervals of 20, using your example how would I calculate the X & Y?
|
|
|
|
|
Ah - to get the length of 20 pixels, you would indeed have to use trig to get the x and y, unless it was a 3,4,5 triangle. ( where one side is 3*i, one is 4*i and the hypotenuse is 5*i ).
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote:
you would indeed have to use trig to get the x and y
lol, I think thats the question I originally asked. I noticed 1 thing:
float opp = (float)Math.Sign(DegToRad(this.angle))*interval;
using Math.Sign instead of Math.Sin - who could get those two mixed up using intellisense...
Anyway, still doesnt work, was wondering if anyone could spot the obvious error.
|
|
|
|
|
I've create an object:
class myClass{
int value;
string name;
}
now i've created an arraylist of it
ArrayList[] myArray = new ArrayList[4];
now i create multiarray of it:
for(int i=0; i<4; i++){
myArray[i] = new ArrayList[7];
}
now i assign value in this way
...
myClass myClassRef = new myClass();
myClassRef.name = "James";
myArray[2][4] = myClassRef;
...
ok now i want to save all myArray inside a text file and then read back again a new myNewArray.
How? Can you show me a bit of code?
I choose arraylist becouse it is not definited the dimension.
I don't want to use XML becouse C# is a full self sufficient language!
Tnx to all.
|
|
|
|
|
Add a custom serializer and deserializer to the class, then. Something like:
class myClass{<br />
<br />
public int value;<br />
public string name;<br />
<br />
public MyClass() {}<br />
<br />
public MyClass(string serializedData) {<br />
int pos = serializedData.FirstIndexOf(' ');<br />
this.value = int.Parse(serializedData.Substring(0, pos));<br />
this.name = serializedData.SubString(pos + 1);<br />
}<br />
<br />
public string Serialize() {<br />
return this.value.ToString() + " " + this.name;<br />
}<br />
<br />
}
Then just loop through the arrays and write the serialized string of each object to the file (using WriteLine).
When you wish to recreate the arrays, read each line and use the deserializer constructor to recreate each object.
---
b { font-weight: normal; }
|
|
|
|
|
tnx for your answer but it is not the way i choose...
|
|
|
|
|
What way did you choose, then?
---
b { font-weight: normal; }
|
|
|
|
|
Sasuko wrote:
I don't want to use XML becouse C# is a full self sufficient language!
Huh? So you won't use SQL because C# is a "full sufficient language!"? XML is designed for storing data, C# is not, so what's the point in staying away from XML?
You can use Binary serialization if you want to, just serialize the arraylist using BinaryFormatter and deserialize it back.
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
You can use Binary serialization if you want to, just serialize the arraylist using BinaryFormatter and deserialize it back.
Yes that is the way, but i need example of code.
|
|
|
|
|
Check out these two functions for serializing and deserializing data using a BinaryFormatter. It serializes a hashtable but can be replaced by an arraylist and should work just fine.
static void Serialize() <br />
{<br />
Hashtable addresses = new Hashtable();<br />
addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");<br />
addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");<br />
addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");<br />
<br />
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);<br />
<br />
BinaryFormatter formatter = new BinaryFormatter();<br />
try <br />
{<br />
formatter.Serialize(fs, addresses);<br />
}<br />
catch (SerializationException e) <br />
{<br />
Console.WriteLine("Failed to serialize. Reason: " + e.Message);<br />
throw;<br />
}<br />
finally <br />
{<br />
fs.Close();<br />
}<br />
}<br />
<br />
<br />
static void Deserialize() <br />
{<br />
Hashtable addresses = null;<br />
<br />
FileStream fs = new FileStream("DataFile.dat", FileMode.Open);<br />
try <br />
{<br />
BinaryFormatter formatter = new BinaryFormatter();<br />
<br />
addresses = (Hashtable) formatter.Deserialize(fs);<br />
}<br />
catch (SerializationException e) <br />
{<br />
Console.WriteLine("Failed to deserialize. Reason: " + e.Message);<br />
throw;<br />
}<br />
finally <br />
{<br />
fs.Close();<br />
}<br />
<br />
foreach (DictionaryEntry de in addresses) <br />
{<br />
Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);<br />
}<br />
}
Elvis (a.k.a Azerax)
Life is Music listen to it before it fades
|
|
|
|
|
Hello,
I would like to mask the text box such that it would only be for the money amounts, and it would have $ sign in front. So for input in textbox:
4500
Masked to: $ 4,500
Any ideas?
Thank you.
|
|
|
|
|
You could just format the number before you display it.
double x = 4500.00;
textBox2.Text= x.ToString("c");
output = $4,500.00
|
|
|
|
|
This the modification version of MaskedTextControl from
User Interfaces in C# book
using System;
using System.Windows.Forms;
public class MaskedTextBox : TextBox
{
private string mask;
public string Mask
{
get
{
return mask;
}
set
{
mask = value;
this.Text = "";
}
}
Protected override void OnKeyPress(KeyPressEventArgs e)
{
if (Mask != "")
{
e.Handled = true;
string newText = this.Text;
bool finished = false;
for (int i = this.SelectionStart; i < mask.Length; i++)
{
switch (mask[i].ToString())
{
case "#" :
if (Char.IsDigit(e.KeyChar) )
{
if(this.TextLength<mask.Length)
{
newText += e.KeyChar.ToString();
finished = true;
break;
}
else
{
newText=newText.Remove(i,this.SelectionLength);
newText=newText.Insert(i,e.KeyChar.ToString());
finished = true;
break;
}
}
else
{
return;
}
default :
newText += mask[i];
break;
}
if (finished)
{ break; }
}
this.Text = newText;
this.SelectionStart = this.Text.Length;
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
e.Handled = true;
}
}
In the Form Load event add the following
maskTextBox.Mask = "$###,###";
MCAD
|
|
|
|
|
There seems to be an error:
if(this.TextLength {
in this line, the IF statement is not completed, or am I reading it incorectly? Is it:
if (this.TextLength != 0)
Another thing, to make the amount bigger, do I just increase the mask line: "$###,###,###....", or there is a better way?
Thank you
|
|
|
|
|
sorry some thing wrong was happend
replace if(this.TextLength {
<br />
with<pre><code><br />
if(this.TextLength<mask.Length)
This prevent the user form enter number longer than mask lenght
zaboboa wrote:
Another thing, to make the amount bigger, do I just increase the mask line: "$###,###,###....", or there is a better way?
Yes you cab write mask like $###,###,### or even
$(###)(###),(###)
Note:this quick code so test it before you use it
and you may add another thibgs like backspace...
MCAD
|
|
|
|
|
You know, it will work better if you put this statement insted
if(this.TextLength == this.SelectionStart)
Of course for this mask to be perfect you have to add a Leave event, in case the user leaves the box, with the input like that $455,6
Thank you for your help, it gave me a big jumpstart.
|
|
|
|
|
zaboboa wrote:
Of course for this mask to be perfect you have to add a Leave event, in case the user leaves the box, with the input like that $455,6
And Copy and Paste there are many things you can add it
Go on
MCAD
|
|
|
|
|
has anyone come across an open source server in C# or any other language..
I need one....I'll convert it into C# if its not in C#
|
|
|
|
|
|
i have a XML file and i wish to remove a few nodes from it...I am using the XMLDocument class...How can i do this without loading it into a dataset or something..
|
|
|
|
|
What exaclty are you trying to do. Permanently remove nodes from the document? Copy nodes to nodes outside the document?
|
|
|
|