Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3602008
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:41:31+00:00 2026-05-18T20:41:31+00:00

I have (with assistance) created a function that plots and draws a line of

  • 0

I have (with assistance) created a function that plots and draws a line of blocks within a 3D space. Generally this is performed in a 64x64x64 gridded cube.

This is the code I have:

    internal static int DrawLine(Player theplayer, Byte drawBlock,
                                 int x0, int y0, int z0, int x1, int y1, int z1)
    {
        int blocks = 0;
        bool cannotUndo = false;
        bool detected = false;

        int dx = x1 - x0;
        int dy = y1 - y0;
        int dz = z1 - z0;

        DrawOneBlock(theplayer, drawBlock, x0, y0, z0, ref blocks, ref cannotUndo);
        if (Math.Abs(dx) > Math.Abs(dy) &&
            Math.Abs(dx) > Math.Abs(dz) && 
            detected == false)
        {
            detected = true;
            float my = (float)dy / (float)dx;
            float mz = (float)dz / (float)dx;
            float by = y0 - my * x0;
            float bz = z0 - mz * x0;
            dx = (dx < 0) ? -1 : 1;
            while (x0 != x1)
            {
                x0 += dx;
                DrawOneBlock(theplayer, drawBlock,
                    Convert.ToInt32(x0),
                    Convert.ToInt32(Math.Round(my * x0 + by)),
                    Convert.ToInt32(Math.Round(mz * x0 + bz)),
                    ref blocks, ref cannotUndo);
            }
        }
        if (Math.Abs(dy) > Math.Abs(dz) && 
            Math.Abs(dy) > Math.Abs(dx) &&
            detected == false)
        {
            detected = true;
            float mz = (float)dz / (float)dy;
            float mx = (float)dx / (float)dy;
            float bz = z0 - mz * y0;
            float bx = x0 - mx * y0;
            dy = (dy < 0) ? -1 : 1;
            while (y0 != y1)
            {
                y0 += dy;
                DrawOneBlock(theplayer, drawBlock,
                             Convert.ToInt32(Math.Round(mx * y0 + bx)),
                             Convert.ToInt32(y0),
                             Convert.ToInt32(Math.Round(mz * y0 + bz)),
                             ref blocks, ref cannotUndo);
            }
        }
        if (detected == false)
        {
            detected = true;
            float mx = (float)dx / (float)dz;
            float my = (float)dy / (float)dz;
            float bx = x0 - mx * z0;
            float by = y0 - my * z0;
            dz = (dz < 0) ? -1 : 1;
            while (z0 != z1)
            {
                z0 += dz;
                DrawOneBlock(theplayer, drawBlock,
                             Convert.ToInt32(Math.Round(mx * z0 + bx)),
                             Convert.ToInt32(Math.Round(my * z0 + by)),
                             Convert.ToInt32(z0),
                             ref blocks, ref cannotUndo);
            }
        }
        return blocks;
    }

It should queue up the block drawing and return the number of blocks it has drawn. The problem is that it is not drawing an un-broken line. In certain instances it leaves gaps between the blocks when at the very least all blocks should be connected by their vertices.

The only part of the code I struggled with is that I was calculating the largest difference in axis and creating a slope constant. I ran into an issue when trying to do a perfect diagonal line. All values were equal so I just defaulted to the z axis – this is where I believe the issue exists.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:41:32+00:00Added an answer on May 18, 2026 at 8:41 pm

    Maybe the Bresenham line algorithm modified to (hopefully) work in 3D could be an alternative for you?

    public static void Swap<T>(ref T x, ref T y)
    {
        T tmp = y;
        y = x;
        x = tmp;
    }
    
    private void Draw3DLine(int x0, int y0, int z0, int x1, int y1, int z1)
    {
        bool steepXY = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);
        if (steepXY) { Swap(ref x0, ref y0); Swap(ref x1, ref y1); }
    
        bool steepXZ = Math.Abs(z1 - z0) > Math.Abs(x1 - x0);
        if (steepXZ) { Swap(ref x0, ref z0); Swap(ref x1, ref z1); }
    
        int deltaX = Math.Abs(x1 - x0);
        int deltaY = Math.Abs(y1 - y0);
        int deltaZ = Math.Abs(z1 - z0);
    
        int errorXY = deltaX / 2, errorXZ = deltaX / 2;
    
        int stepX = (x0 > x1) ? -1 : 1;  
        int stepY = (y0 > y1) ? -1 : 1;
        int stepZ = (z0 > z1) ? -1 : 1;
    
        int y=y0, z=z0;
    
        // Check if the end of the line hasn't been reached.
        for(int x = x0; x!=x1; x+=stepX) 
        {
            int xCopy=x, yCopy=y, zCopy=z;
    
            if (steepXZ) Swap(ref xCopy, ref zCopy);
            if (steepXY) Swap(ref xCopy, ref yCopy);
    
            // Replace the WriteLine with your call to DrawOneBlock
            Console.WriteLine("[" + xCopy + ", " + yCopy + ", " + zCopy + "], ");
    
            errorXY -= deltaY;
            errorXZ -= deltaZ;
    
            if (errorXY < 0) 
            {
                y += stepY;
                errorXY += deltaX;
            }
    
            if (errorXZ < 0) 
            {
                z += stepZ;
                errorXZ += deltaX;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have (with assistance) created a function that plots and draws a line of
Does anyone have any recommendations of tools that can be of assistance with moving
I have created an application that uses Microsoft.Office.Interop.Excel, in my local and testing environments
I have been attempting to develop an embedded webserver within an application that I
I have many JUnit tests, which are all created by Netbeans' assistant (so nothing
I have a django application that creates xls and txt files. I'm trying to
I have the following function which uses the old Bassistance autocomplete: $('#searchLocation').autocomplete(/Utils.aspx, { dataType:
I have a form with multiple fields that I'm validating (some with methods added
I am working on a DLL that a webservice I have uses, I added
I'm working on a searching function for my database. I've created a test DB

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.