简体   繁体   English

如何使用 C 在 bitmap 图像上创建对角线

[英]How to create a diagonal line on bitmap image with C

How do I create a diagonal line with C, I want to get four coordinates and create the most perfect diagonal line, but I want to draw it pixel by pixel.如何用C创建对角线,我想得到四个坐标并创建最完美的对角线,但我想逐个像素地绘制它。 The problem is I don't know how to do this when the image is not "a square".问题是当图像不是“正方形”时,我不知道该怎么做。

What I have till now:到目前为止我所拥有的:

// try one
    for (int i = ystart, j = xstart; i < yend && j < xend; i++, j++)
    {
        printf("%d ", distance);
        image[i][j].rgbtRed = 255;
        image[i][j].rgbtGreen = 255;
        image[i][j].rgbtBlue = 255;
    }

// try two
    int distance = sqrt(pow(xend - xstart, 2) + pow(yend - ystart, 2));

    for (int w = 0, i = ystart, j = xstart; w < distance; w++)
    {
        image[i][j].rgbtRed = 255;
        image[i][j].rgbtGreen = 255;
        image[i][j].rgbtBlue = 255;

        i++;
        j++;

        if (i > yend - 1 || j > xend - 1)
        {
            break;
        }
    }


// I also have a two-dimensional (the image) array and width and height of the image, the image I'm using is 600x400, completely black and I want a diagonal line based on the parameters of the function

Use the line formula from middle-school algebra class, "y = mx + b", and calculate the ideal y for each x.使用中学代数 class 中的直线公式,“y = mx + b”,并计算每个 x 的理想 y。

To draw a line from (1, 7) to (6, 10) you might do this:要从 (1, 7) 到 (6, 10) 画一条线,您可以这样做:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

void draw_pixel (int x, int y) {
  printf("draw pixel at (%d, %d)\n", x, y);
};

void main () {

  float m = (float) (10 - 7) / (6 - 1);

  float y = 7;
  for (int x = 1; x <= 6; x++) {
    draw_pixel(x, (int) round(y));
    y += m;
  };

};

Which does this:这样做:

draw pixel at (1, 7)
draw pixel at (2, 8)
draw pixel at (3, 8)
draw pixel at (4, 9)
draw pixel at (5, 9)
draw pixel at (6, 10)

You'll note that this doesn't work so well when abs(x2 - x1) < abs(y2 - y1) as it won't draw a continuous line.您会注意到,当abs(x2 - x1) < abs(y2 - y1)时,这并不能很好地工作,因为它不会画出一条连续的线。 To solve that, you have to use a different formula, x = my + b , which is accomplished in the same way but by swapping every x and y value, so that the for() loops over each y value and calculating the x, instead of looping over each x value and calculating the y.为了解决这个问题,您必须使用不同的公式x = my + b ,它以相同的方式完成,但通过交换每个 x 和 y 值,以便for()循环每个 y 值并计算 x,而不是遍历每个 x 值并计算 y。

Drawing it using a naive line-drawing algorithm using the formula for a line in plane won't look well:使用平面线的公式使用简单的线条绘制算法绘制它看起来不太好:

dx = x2 − x1
dy = y2 − y1
for x from x1 to x2 do
    y = y1 + dy*(x − x1)/dx
    matrix[x, y] = BLACK

It works if you want to do the homework, but it does not work if you want to make a good software.如果你想做功课,它可以工作,但如果你想制作一个好的软件,它就行不通。 In the past, with the old diplays, this was a problem.过去,对于旧显示器,这是一个问题。 It was difficult to make a program to draw a line that was visible like a line.很难编写一个程序来绘制一条像线一样可见的线。

There are many algorithms to do it.有很多算法可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM