|
-
Feb 28th, 2009, 05:10 AM
#1
Thread Starter
Fanatic Member
New Class question
I have what is probably a very simple question
I have some work to do with points and lines ( not drawing just from a math point of view)
There is already an object called point
i want to make a new class called Line where i would initializes it with two points? The end game is to then be able to run a method or make a property where i could get information out of it about the line the two points create
Thanks
Crash893
-
Feb 28th, 2009, 05:25 AM
#2
Thread Starter
Fanatic Member
Re: New Class question
correction Constructor is the word i was looking for ( that's probably why i couldn't find what i was looking for)
Here is my code thus far (it doesn't do anything yet but please let me know if im doing anything wrong ( or right))
c# Code:
using System.Drawing;
using System;
namespace WindowsFormsApplication1
{
class Line
{
Point aP1 = new Point();
Point aP2 = new Point();
public Line(Point P1, Point P2)
{
aP1 = P1;
aP2 = P2;
}
public Point GetRandP()
{
Point P3 = new Point();
Random RandP = new Random();
P3.X = RandP.Next();
P3.Y= RandP.Next();
return P3;
}
}
}
here is the code in the form
c# Code:
Point P1 = new Point();
P1.X = 8;
P1.Y = 9;
Point P2 = new Point();
P1.X = 56;
P1.Y = 0;
Line L1 = new Line(P1, P2);
Point P3 = new Point();
P3 = L1.GetRandP();
-
Feb 28th, 2009, 05:35 AM
#3
Re: New Class question
It looks good. Two points is all that takes to construct a line and thats what you take in the constructor, so all is good there.
From an object oriented design point of view though, I would say that the GetRandomP method doesnt really belong in the Line class, as it seems to be totally unrelated the concept of "A line".
If I where you I'd actually create a new class for the Point even though there is already a structure for Point. I would then make the GetRandomP a static method of that class. This would look better from a OO design point of view.
-
Feb 28th, 2009, 05:40 AM
#4
Thread Starter
Fanatic Member
Re: New Class question
the getrandp mehod is just there for show at this point
The general idea is that i enter two points then i can ask for it retreive a random point along the line that is created so it does have something to do with the that partiular line that i created
the math is shaping up to look something like this
int dist = random.next();
Point pt3 = new Point((pt1.X + dist) * (pt2.X - pt1.X), (pt1.Y + dist) * (pt2.Y - pt1.Y));
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|