Results 1 to 4 of 4

Thread: New Class question

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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

  2. #2

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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:
    1. using System.Drawing;
    2. using System;
    3. namespace WindowsFormsApplication1
    4. {
    5.     class Line
    6.     {
    7.         Point aP1 = new Point();
    8.         Point aP2 = new Point();
    9.  
    10.         public Line(Point P1, Point P2)
    11.         {
    12.             aP1 = P1;
    13.             aP2 = P2;
    14.        
    15.         }
    16.         public Point GetRandP()
    17.         {
    18.             Point P3 = new Point();
    19.             Random RandP = new Random();
    20.             P3.X = RandP.Next();
    21.             P3.Y= RandP.Next();
    22.             return P3;
    23.         }
    24.     }
    25. }


    here is the code in the form

    c# Code:
    1. Point P1 = new Point();
    2.             P1.X = 8;
    3.             P1.Y = 9;
    4.  
    5.             Point P2 = new Point();
    6.             P1.X = 56;
    7.             P1.Y = 0;
    8.  
    9.             Line L1 = new Line(P1, P2);
    10.  
    11.  
    12.             Point P3 = new Point();
    13.             P3 = L1.GetRandP();

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    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
  •  



Click Here to Expand Forum to Full Width