CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    C++: how get line points?

    using the line point origin and destination, we can calculate the line dots between origin and destination:
    Code:
    #define M_PI           3.14159265358979323846  /* pi */
    
    struct Position2D
    {
        Position2D()
        {
            //Nothing;
        }
    
    
        Position2D(float X, float Y)
        {
            PosX = X;
            PosY = Y;
        }
        float PosX = 0;
        float PosY = 0;
    };
    
    
    struct Position3D
    {
        float PosX = 0;
        float PosY = 0;
        float PosZ = 0;
    
    
        Position3D()
        {
            //Nothing;
        }
    
    
        Position3D(float X, float Y, float Z)
        {
            PosX = X;
            PosY = Y;
            PosZ = Z;
        }
    
    
        operator Position2D()
        {
            Position2D Pos2D{PosX, PosY};
            Pos2D.PosX = PosX;
            Pos2D.PosY = PosY;
            return Pos2D;
        }
    
    
    };
    
    
    std::vector<Position3D> GetLinePoints(Position3D Origin, Position3D Destination)
    {
        //Getting the Line Points Count:
        int LineCountPoints = sqrt(abs(Destination.PosX - Origin.PosX) * abs(Destination.PosX - Origin.PosX)
                                      + abs(Destination.PosY - Origin.PosY) * abs(Destination.PosY - Origin.PosY)
                                      + abs(Destination.PosZ - Origin.PosZ) * abs(Destination.PosZ - Origin.PosZ));
        
        //Get the increment for X, Y and Z:
        Position3D Increment;
        Increment.PosX = (Destination.PosX - Origin.PosX) / LineCountPoints;
        Increment.PosY = (Destination.PosY - Origin.PosY) / LineCountPoints;
        Increment.PosZ = (Destination.PosZ - Origin.PosZ) / LineCountPoints;
    
    
        Position3D NextPoint=Origin;
    
    
        std::vector<Position3D> GetPoints;
        for(int LinePoint =0 ; LinePoint<LineCountPoints; LinePoint++)
        {
            //Get the next point:
            NextPoint.PosX += Increment.PosX;
            NextPoint.PosY += Increment.PosY;
            NextPoint.PosZ += Increment.PosZ;
            
            //Round the values:
            Position3D Pos3D;
            Pos3D.PosX = round(NextPoint.PosX);
            Pos3D.PosY = round(NextPoint.PosY);
            Pos3D.PosZ = round(NextPoint.PosZ);
            
            //Add the 3D position on vector:
            GetPoints.push_back(Pos3D);
        }
        return GetPoints;
    }
    my problem is some point\pixel can be more 1 X or Y... how can i precise more the line points?
    see these red line:

    (i use the rotation calculation for rotate the origin and destination)
    can i make it more precise?
    i need be more precise for fill a rectangle without black pixels... can anyone advice me?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: C++: how get line points?

    Some questions to your post:
    1. What do you mean by "precise" / "more precise"?
    2. What do you mean by "line dots / points"?
    3. If you just want to "fill a rectangle" the why not use FillRect function?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: C++: how get line points?

    Quote Originally Posted by VictorN View Post
    Some questions to your post:
    1. What do you mean by "precise" / "more precise"?
    2. What do you mean by "line dots / points"?
    3. If you just want to "fill a rectangle" the why not use FillRect function?
    hello VictorN.. sometime
    1 - the line be printed more clear(mean the perfect line);
    2 - the line is printed using the pixels positions... the positions must be calculated. the 'GetLinePoints()' do the job.
    3 - because the FillRect() don't draw like the 3D.. we must use the same height, but on 3D it can be different.

    https://imgur.com/Ftpb6H9

  4. #4
    Join Date
    Apr 2009
    Posts
    1,355

    Re: C++: how get line points?

    see these image:
    Name:  imagem_2022-03-27_201609.png
Views: 110
Size:  375 Bytes


    the line can be more precise? is more like a stairs than a line

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

Featured