鱼C论坛

 找回密码
 立即注册
查看: 1486|回复: 9

[已解决]求解用数组和动态数组来画比特地图问题(bitmap)

[复制链接]
发表于 2019-11-5 08:58:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 丶不离不弃 于 2019-11-6 10:32 编辑

最近老师布置了一个有加分的题目就是让我们用C++来画bitmap,老师的要求如下:
  1. int main() {
  2.     // FIRST PART (ADD COLORS HERE)
  3.     constexpr uint32_t BLACK = 0xFF000000; // black opaque
  4.     constexpr uint32_t RED =   0xFF0000FF; // red opaque
  5.     constexpr uint32_t BLUE =  0xFFFF0000;//blue opaque
  6.     constexpr uint32_t WHITE = 0xFF0000FF;
  7.     constexpr uint32_t YELLOW =   0xFF0000FF;

  8.     // SECOND PART (DO NOT EDIT)
  9.     int xcenter = 100;
  10.     int ycenter = 100;
  11.     int xdiameter = 200;
  12.     int ydiameter = 100;
  13.     Bitmap b(BLACK); // Hardcoded size (***800 x 600 pixels***)
  14.     b.horizLine(0, 500, 200, RED); // Red horizontal line, from x=0 to x=500, at y = 200
  15.     b.vertLine(0, 399, 300, RED); // Red vertical line, from y=0 to y=399, at x = 300
  16.     b.drawRect(200,200, 100,50, BLUE); // Blue rectangle, TOP-LEFT at x=200, y=200. width=100, height=50
  17.     b.fillRect(201,201, 98,48, WHITE); // White rectangle, same rules as above, but filled with color
  18.     b.line(400,0, 550,300, YELLOW); // Line drawn using https://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  19.     b.ellipse(xcenter, ycenter, xdiameter, ydiameter); //Ellipse using specs from above
  20.     b.save("bitmap.png");
  21.     // THIRD PART - OPTIONAL FUNCTION
  22.     // 100pt bonus for properly implementing Wu's antialiasing
  23.     //https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm
  24.     //b.antialiasedLine(400,0, 550,300, YELLOW);
  25. }
复制代码


老师要求我们写的程序中要有头文件,我在github找了半天找到了个,但是我编译了一下有错https://github.com/StevensDeptEC ... b/stb_image_write.h
然后我写的程序部分就是用布雷森汉姆直线画法以及画椭圆部分不是特别明白,请问有大神可以帮我解决一下吗?谢谢!
我的代码如下:
  1. #include <iostream>
  2. #include <cmath>
  3. #define STB_IMAGE_WRITE_IMPLEMENTATION
  4. #include <cstdint>
  5. using namespace std;
  6. #include "stb_image_write.h"
  7. #include "stb.image.h"
  8. #define x0 400       //定义全局变量x0,y0:坐标轴中心(x0,y0)
  9. #define y0 300
  10. class Bitmap{
  11. private:
  12.     constexpr static uint32_t w=800;
  13.     constexpr static uint32_t h=600;
  14.     uint32_t pixels[w][h];
  15. public:
  16.     Bitmap(uint32_t v){
  17.         for(uint32_t i=0; i<h; i++){
  18.             for(uint32_t j=0; j<w; j++){
  19.                 pixels[i][j]=v;
  20.             }
  21.         }
  22.     }
  23.     void horizLine(uint32_t x1, uint32_t x2, uint32_t y, uint32_t v) {
  24.         for (uint32_t i = x1; i <= x2; i++) {
  25.             pixels[y][i]=v;
  26.         }
  27.     }
  28.     void vertLine(uint32_t y1, uint32_t y2, uint32_t x, uint32_t v){
  29.         for(uint32_t j=y1; j<=y2; j++){
  30.             pixels[j][x]=v;
  31.         }
  32.     }
  33.     void fillRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h,uint32_t v){
  34.         for(uint32_t i=y; i<=y+h; i++){
  35.             for(uint32_t j=x; j<=x+w; j++){
  36.                 pixels[i][j]=v;
  37.             }
  38.         }
  39.     }
  40.     void drawRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h,uint32_t v){
  41.         for(uint32_t i=y; i<=y+w; i++){
  42.             pixels[x][i]=v;
  43.             pixels[x+h][i]=v;
  44.         }
  45.         for(uint32_t j=x; j<=x+h; j++){
  46.             pixels[j][y]=v;
  47.             pixels[j][y+w]=v;
  48.         }
  49.     }
  50.     void line(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t v) {
  51.         /*
  52.         int Dx = x2 - x1;
  53.         int Dy = y2 - y1;
  54.         bool flag = false;
  55.         if (abs(Dy) > abs(Dx)) {
  56.             flag = true;
  57.         }
  58.         if (flag) {
  59.             swap(x1, y1);
  60.             swap(x2, y2);
  61.         }
  62.         if (x1 > x2) {
  63.             swap(x1, x2);
  64.             swap(y1, y2);
  65.         }
  66.         Dx = x2 - x1;
  67.         int a = y2 - y1;
  68.         Dy = abs(a);
  69.         int Er = Dx / 2;
  70.         double Der = Dy / Dx;
  71.         int Ystep;
  72.         int endy = y1;
  73.          */
  74.         int dx=x2-x1;//gain x
  75.         int dy=y2-y1;//gain y
  76.         int q=(2*dy)-dx;
  77.         int doubleDy=2*dy;
  78.         int doubleD=2*(dy-dx);
  79.         int pointx;
  80.         int pointy;
  81.         //set two variable to display position
  82.         if(x1>x2){
  83.             pointx=x2;
  84.             pointy=y2;
  85.             x2=x1;
  86.         }
  87.         else{
  88.             pointx=x1;
  89.             pointy=y1;
  90.         }
  91.         while(pointx<x2){
  92.             pointx++;
  93.             if(q<0){
  94.                 q+=doubleDy;
  95.             }
  96.             else{
  97.                 pointy++;
  98.                 q+=doubleD;
  99.             }
  100.             for(uint32_t i=x1;i<=x2; i++){
  101.                 for(uint32_t j=y1; j<=y2;j++){
  102.                     pixels[i][j]=v;
  103.                 }
  104.             }
  105.         }
  106.     }


  107.     void ellipse(uint32_t xcenter, uint32_t ycenter, uint32_t xdiameter, uint32_t ydiameter, uint32_t v) {
  108.         int X;
  109.         int Y;
  110.         int a = xdiameter / 2;
  111.         int b = ydiameter / 2;
  112.         const double PI = 3.14;
  113.         double del = 0.001;
  114.         for (double i = -PI; i < PI; i = i + del) {
  115.             Y = ycenter + b * sin(i);
  116.             X = xcenter + a * cos(i);
  117.             pixels[Y][X] = v;
  118.         }
  119.     }


  120.     void save(const char filename[]) {
  121.         stbi_write_png(filename, w, h, 4, pixels, w*4);
  122.     }

  123. };

  124. int main() {
  125.     // FIRST PART (ADD COLORS HERE)
  126.     constexpr uint32_t BLACK = 0xFF000000; // black opaque
  127.     constexpr uint32_t RED =   0xFF0000FF; // red opaque
  128.     constexpr uint32_t BLUE =  0xFFFF0000;//blue opaque
  129.     constexpr uint32_t WHITE = 0xFF0000FF;
  130.     constexpr uint32_t YELLOW =0xFF0000FF;
  131.     constexpr uint32_t GREEN =0xFF0000FF;


  132.     // SECOND PART (DO NOT EDIT)
  133.     int xcenter = 100;
  134.     int ycenter = 100;
  135.     int xdiameter = 200;
  136.     int ydiameter = 100;
  137.     Bitmap b(BLACK); // Hardcoded size (***800 x 600 pixels***)
  138.     b.horizLine(0, 500, 200, RED); // Red horizontal line, from x=0 to x=500, at y = 200
  139.     b.vertLine(0, 399, 300, RED); // Red vertical line, from y=0 to y=399, at x = 300
  140.     b.drawRect(200,200, 100,50, BLUE); // Blue rectangle, TOP-LEFT at x=200, y=200. width=100, height=50
  141.     b.fillRect(201,201, 98,48, WHITE); // White rectangle, same rules as above, but filled with color
  142.     b.line(400,0, 550,300, YELLOW); // Line drawn using https://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  143.     b.ellipse(xcenter, ycenter, xdiameter, ydiameter,GREEN); //Ellipse using specs from above
  144.     b.save("bitmap.png");
  145.     // THIRD PART - OPTIONAL FUNCTION
  146.     // 100pt bonus for properly implementing Wu's antialiasing
  147.     //https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm
  148.     //b.antialiasedLine(400,0, 550,300, YELLOW);
  149. }
复制代码





此外我还有如下问题:

1.下面这部分代码是用二维数组来做的,直线画法中如果用吴晓琳直线画法有额外的加分,但是我看了很久都不是特别会。

2.老师还有个建议是用动态数组来做,这个我到现在还没头绪,请问有人能教下我咋写的吗?老师上课的例子如下:

  1. #include <iostream>
  2. #include <cstdint>
  3. using namespace std;
  4. class Bitmap {
  5. private:
  6.     uint32_t w, h;
  7.     uint32_t* b;
  8. public:
  9.     Bitmap(uint32_t w, uint32_t h, uint32_t pixel)
  10.             : w(w), h(h), b(new uint32_t[w*h])
  11.     {
  12.         for (uint32_t i = 0; i < w*h; i++)
  13.             b[i] = pixel;
  14.     }
  15.     Bitmap() : w(0), h(0), b(nullptr) {}
  16.     ~Bitmap() {
  17.         delete [] b;
  18.     }
  19.     Bitmap(const Bitmap& orig) :
  20.             w(orig.w), h(orig.h), b(new uint32_t[orig.w*orig.h]) {
  21.         memcpy(b, orig.b, sizeof(uint32_t) * w * h);
  22.     }
  23. #if 0
  24.     this is the old way!!
  25.     Bitmap& operator =(const Bitmap& orig) {
  26.                 if (this != &orig) {
  27.                         delete [] b;
  28.                         w = orig.w;
  29.                         h = orig.h;
  30.                         b = new uint32_t[w*h];
  31.                         memcpy(b, orig.b, sizeof(uint32_t) * w * h);
  32.                 }
  33.                 return *this;
  34.         }
  35. #endif
  36.     //modern approach: copy and swap
  37.     Bitmap& operator =(Bitmap orig) {
  38.         w = orig.w;
  39.         h = orig.h;
  40.         swap(b, orig.b);
  41.         return *this;
  42.     }
  43.     Bitmap(Bitmap&& orig) : w(orig.w), h(orig.h), b(orig.b) {
  44.         orig.b = nullptr;
  45.     }
  46. };
  47. Bitmap makemeapicture() {
  48.     Bitmap b(1024, 768, 0x0);
  49.     return b;
  50. }
  51. int main() {
  52.     Bitmap b(100,200, 0xFF000000);
  53.     Bitmap b2(b); //copy constructor
  54.     Bitmap b3(150,100, 0xFFFF);
  55.     b3 = b2;
  56.     b3 = b3;
  57.     b2 = b3 = b;
  58. }
复制代码
最佳答案
2019-11-7 15:14:28
本帖最后由 superbe 于 2019-11-7 15:18 编辑

一些修改

  1.     void fillRect(uint32_t x, uint32_t y,uint32_t w, uint32_t h, uint32_t v){
  2.         /*for(uint32_t i=x; i<=y+h; i++){
  3.             for(uint32_t j=y; j<= x+w; j++){
  4.                 m[i*j]=v;
  5.             }
  6.         }*/
  7.         for(uint32_t i = x; i <= x + w; i++)
  8.             for(uint32_t j = y; j <= y + h; j++){
  9.                 m[i + j * cols] = v;
  10.             }
  11.         
  12.     }

  13.     /* 这个函数有问题,画出的线位置不对,两个程序的line代码都改掉。参考Bresenham算法链接里的伪代码修改吧。
  14.        或参考我上面程序的line代码(也是照链接改的)。吴小林算法链接里也有伪代码,也照着编写吧。 */
  15.     void line(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t v){
  16.           。。。
  17.     }

  18.     void ellipse(uint32_t xcenter, uint32_t ycenter, uint32_t xdiameter, uint32_t ydiameter, uint32_t v){
  19.             uint32_t X;
  20.             uint32_t Y;
  21.             uint32_t  a = xdiameter / 2;
  22.             uint32_t b = ydiameter / 2;
  23.             const double PI = 3.14;
  24.             double del = 0.001;
  25.             for (double i = -PI; i < PI; i = i + del) {
  26.                 Y = ycenter + b * sin(i);
  27.                 X = xcenter + a * cos(i);
  28.                 //m[Y*rows+X] = v;  //这行不对,改成下行
  29.                 m[Y * cols + X] = v;
  30.             }
  31.     }
  32.     void save(const char filename[]) {
  33.         //stbi_write_png(filename, rows, cols, 4, m, rows*cols * 4);
  34.         stbi_write_png(filename, cols, rows, 4, m, cols * 4);
  35.     }
复制代码


修改后运行测试,只有line画线不正确。

head file.rar

75.7 KB, 下载次数: 4

这是2个头文件

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-11-6 12:53:05 | 显示全部楼层
顶一下~~~5555竟然木有人回答~~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-6 13:49:42 | 显示全部楼层
比特地图是什么鬼?!那玩意不是叫 位图 吗?.bmp 格式的文件。
注意:我就是来吐个槽的,不要找我讨论问题的解答,没学多细致。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-7 00:03:23 | 显示全部楼层
本帖最后由 superbe 于 2019-11-7 21:12 编辑
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdint>
  4. #define STB_IMAGE_WRITE_IMPLEMENTATION
  5. #include "stb_image_write.h"
  6. #include "stb_image.h"
  7. #define x0 400       //定义全局变量x0,y0:坐标轴中心(x0,y0)
  8. #define y0 300
  9. using namespace std;

  10. class Bitmap{
  11. private:
  12.     constexpr static uint32_t w = 800;
  13.     constexpr static uint32_t h = 600;
  14.    uint32_t pixels[h][w];
  15. public:
  16.     Bitmap(uint32_t v){
  17.         for (int i = 0; i < h; i++)
  18.             for(int j = 0; j < w; j++)
  19.                 pixels[i][j] = v;
  20.     }

  21.     void horizLine(int x1, int x2, int y, uint32_t v) {
  22.         for (int i = x1; i <= x2; i++) {
  23.             pixels[y][i] = v;
  24.         }
  25.     }

  26.     void vertLine(int y1, int y2, int x, uint32_t v){
  27.         for(int j = y1; j <= y2; j++){
  28.             pixels[j][x] = v;
  29.         }
  30.     }

  31.     void fillRect(int x, int y, int w1, int h1, uint32_t v){
  32.         for(int i = x; i <= x + w1; i++){
  33.             for(int j = y; j <= y + h1; j++){
  34.                 pixels[j][i] = v;
  35.             }
  36.         }
  37.     }

  38.     void drawRect(int x, int y, int w1, int h1,uint32_t v){
  39.         for(int i = x; i <= x + w1; i++){  //画两条水平线(可调用horizLine)
  40.             pixels[y][i] = v;
  41.             pixels[y + h1][i] = v;
  42.         }
  43.         for(int j = y; j <= y + h1; j++){  //画两条垂直线(可调用vertLine)
  44.             pixels[j][x] = v;
  45.             pixels[j][x + w1] = v;
  46.         }
  47.     }

  48.     void line(int x1, int y1, int x2, int y2, uint32_t v){
  49.         bool steep = abs(y2 - y1) > abs(x2 - x1);
  50.         if(steep){
  51.             swap(x1, y1);
  52.             swap(x2, y2);
  53.         }
  54.         if(x1 > x2){
  55.             swap(x1, x2);
  56.             swap(y1, y2);
  57.         }
  58.         int deltax = x2 - x1;
  59.         int deltay = abs(y2 - y1);
  60.         int error = deltax / 2;
  61.         int ystep = y1 < y2 ? 1 : -1;
  62.         int y = y1;
  63.         for(int x = x1; x <= x2; x++){
  64.             if(steep) pixels[x][y] = v;
  65.             else pixels[y][x] = v;
  66.             error -= deltay;
  67.             if(error < 0){
  68.                 y += ystep;
  69.                 error += deltax;
  70.             }
  71.         }
  72.     }

  73.     void ellipse(int xcenter, int ycenter, int xdiameter, int ydiameter, uint32_t v) {
  74.         int x, y;
  75.         int a = xdiameter / 2;
  76.         int b = ydiameter / 2;
  77.         const double PI = 3.14;
  78.         double del = 0.001;
  79.         for (double i = -PI; i < PI; i = i + del) {
  80.             x = xcenter + a * cos(i);
  81.             y = ycenter + b * sin(i);
  82.             pixels[y][x] = v;
  83.         }
  84.     }

  85.     void save(const char filename[]) {
  86.         stbi_write_png(filename, w, h, 4, pixels, w * 4);
  87.     }

  88. };

  89. int main() {
  90.     //FIRST PART (ADD COLORS HERE)
  91.     constexpr uint32_t BLACK = 0xFF000000; // black opaque
  92.     constexpr uint32_t RED =   0xFF0000FF; // red opaque
  93.     constexpr uint32_t BLUE =  0xFFFF0000; // blue opaque
  94.     constexpr uint32_t WHITE = 0xFFFFFFFF;
  95.     constexpr uint32_t YELLOW =0xFF00FFFF;
  96.     constexpr uint32_t GREEN = 0xFF00FF00;

  97.     // SECOND PART (DO NOT EDIT)
  98.     int xcenter = 100;
  99.     int ycenter = 100;
  100.     int xdiameter = 200;
  101.     int ydiameter = 100;
  102.     Bitmap b(BLACK); // Hardcoded size (***800 x 600 pixels***)
  103.     b.horizLine(0, 500, 200, RED); // Red horizontal line, from x=0 to x=500, at y = 200
  104.     b.vertLine(0, 399, 300, RED);  // Red vertical line, from y=0 to y=399, at x = 300
  105.     b.drawRect(200,200, 100, 50, BLUE); // Blue rectangle, TOP-LEFT at x=200, y=200. width=100, height=50
  106.     b.fillRect(201,201, 98, 48, WHITE); // White rectangle, same rules as above, but filled with color
  107.     b.line(400,0, 550,300, YELLOW); // Line drawn using https://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  108.     b.ellipse(xcenter, ycenter, xdiameter, ydiameter,GREEN); //Ellipse using specs from above
  109.     b.save("bitmap3.png");
  110.     // THIRD PART - OPTIONAL FUNCTION
  111.     // 100pt bonus for properly implementing Wu's antialiasing
  112.     //https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm
  113.     //b.antialiasedLine(400,0, 550,300, YELLOW);
  114. }
复制代码


如果编译提示stb_image_write.h第1696行错误,把该处
#define stbiw__zlib_add(code,codebits) \

      (bitbuf |= (code) << bitcount, bitcount += (codebits), stbiw__zlib_flush())
中间的空行删掉就可以了。
吴小林直线画法待补充。其它的调试通过了。
你详细测试下吧,不确定有没有问题。
bitmap.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2019-11-7 03:57:12 | 显示全部楼层
本帖最后由 丶不离不弃 于 2019-11-7 03:59 编辑
superbe 发表于 2019-11-7 00:03
如果编译提示stb_image_write.h第1696行错误,把该处
#define stbiw__zlib_add(code,codebits) \

  1. #include <iostream>
  2. #include <cmath>
  3. #define STB_IMAGE_WRITE_IMPLEMENTATION
  4. #include <cstdint>
  5. #include "stb_imag.h"
  6. #include "stb_imag_write.h"
  7. using namespace std;
  8. class Bitmap{
  9. private:
  10.     constexpr static uint32_t w=800;
  11.     constexpr static uint32_t h=600;
  12.     uint32_t pixels[h][w];
  13. public:
  14.     Bitmap(uint32_t v){
  15.         for(uint32_t i=0; i<h; i++){
  16.             for(uint32_t j=0; j<w; j++){
  17.                 pixels[i][j]=v;
  18.             }
  19.         }
  20.     }
  21.     void horizLine(uint32_t x1, uint32_t x2, uint32_t y, uint32_t v) {
  22.         for (uint32_t i = x1; i <= x2; i++) {
  23.             pixels[y][i]=v;
  24.         }
  25.     }
  26.     void vertLine(uint32_t y1, uint32_t y2, uint32_t x, uint32_t v){
  27.         for(uint32_t j=y1; j<y2; j++){
  28.             pixels[j][x]=v;
  29.         }
  30.     }
  31.     void fillRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h,uint32_t v){
  32.         for(uint32_t i=y; i<=y+h; i++){
  33.             for(uint32_t j=x; j<=x+w; j++){
  34.                 pixels[i][j]=v;
  35.             }
  36.         }
  37.     }
  38.     void drawRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h,uint32_t v){
  39.         for(uint32_t i=y; i<=y+w; i++){
  40.             pixels[x][i]=v;
  41.             pixels[x+h][i]=v;
  42.         }
  43.         for(uint32_t j=x; j<=x+h; j++){
  44.             pixels[j][y]=v;
  45.             pixels[j][y+w]=v;
  46.         }
  47.     }
  48.     void line(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t v) {
  49.         int x, y, dx, dy, e;
  50.         dx = x2 - x1;
  51.         dy = y2 - y1;
  52.         e = -dx;
  53.         x = x1;
  54.         y = y1;
  55.         for (int i = 0; i <= dx; i++) {
  56.             pixels[y][x] = v;
  57.             x++;
  58.             e = e + 2 * dy;
  59.             if (e >= 0) {
  60.                 y++;
  61.                 e = e - 2 * dx;
  62.             }
  63.         }
  64.     }


  65.     void ellipse(uint32_t xcenter, uint32_t ycenter, uint32_t xdiameter, uint32_t ydiameter, uint32_t v) {
  66.         int X;
  67.         int Y;
  68.         int a = xdiameter / 2;
  69.         int b = ydiameter / 2;
  70.         const double PI = 3.14;
  71.         double del = 0.001;
  72.         for (double i = -PI; i < PI; i = i + del) {
  73.             Y = ycenter + b * sin(i);
  74.             X = xcenter + a * cos(i);
  75.             pixels[Y][X] = v;
  76.         }
  77.     }


  78.     void save(const char filename[]) {
  79.         stbi_write_png(filename, w, h, 4, pixels, w*4);
  80.     }

  81. };

  82. int main() {
  83.     // FIRST PART (ADD COLORS HERE)
  84.     constexpr uint32_t BLACK = 0xFF000000; // black opaque
  85.     constexpr uint32_t RED =   0xFF0000FF; // red opaque
  86.     constexpr uint32_t BLUE =  0xFFFF0000;//blue opaque
  87.     constexpr uint32_t WHITE = 0xFFFFFFFF;//white opaque
  88.     constexpr uint32_t YELLOW =0xFF00FFFF;//yellow opaque
  89.     constexpr uint32_t GREEN = 0xFF008000;//green opaque


  90.     // SECOND PART (DO NOT EDIT)
  91.     int xcenter = 100;
  92.     int ycenter = 100;
  93.     int xdiameter = 200;
  94.     int ydiameter = 100;
  95.     Bitmap b(BLACK); // Hardcoded size (***800 x 600 pixels***)

  96.     b.horizLine(0, 500, 200, RED); // Red horizontal line, from x=0 to x=500, at y = 200
  97.     b.vertLine(0, 399, 300, RED); // Red vertical line, from y=0 to y=399, at x = 300
  98.     b.drawRect(200,200, 100,50, BLUE); // Blue rectangle, TOP-LEFT at x=200, y=200. width=100, height=50
  99.     b.fillRect(201,201, 98,48, WHITE); // White rectangle, same rules as above, but filled with color
  100.     b.line(400,0, 550,300, YELLOW); // Line drawn using https://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  101.     b.ellipse(xcenter, ycenter, xdiameter, ydiameter,GREEN); //Ellipse using specs from above
  102.     b.save("bitmap.png");
  103.     // THIRD PART - OPTIONAL FUNCTION
  104.     // 100pt bonus for properly implementing Wu's antialiasing
  105.     //https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm
  106.     //b.antialiasedLine(400,0, 550,300, YELLOW);
  107. }
复制代码

这是我自己画的哈哈,和你的一样哈哈!!我在你之前做出来了哈啊哈,开心!不过还是要谢谢你呢!
现在就是另一个问题就是老师说的拓展写法用动态数组来画~~~我感觉还是一片空白~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-7 04:03:36 | 显示全部楼层
本帖最后由 丶不离不弃 于 2019-11-7 04:15 编辑
superbe 发表于 2019-11-7 00:03
如果编译提示stb_image_write.h第1696行错误,把该处
#define stbiw__zlib_add(code,codebits) \


我现在在纠结如何用动态数组new int【】来画,就是我后面发的老师给的那个范例,包括用move 构造器,复制(=)构造器等等。你可以帮我看一下嘛~~谢谢。老师说的用吴晓琳画法是在用dynamic memory 方法来写的bitmap加分项目。我今天下午研究下然后尽量在你们起床前发到论坛上我的程序。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-7 06:36:29 | 显示全部楼层
superbe 发表于 2019-11-7 00:03
如果编译提示stb_image_write.h第1696行错误,把该处
#define stbiw__zlib_add(code,codebits) \
  1. #include <cstdint>
  2. #include <iostream>
  3. #include<math.h>
  4. #define STB_IMAGE_WRITE_IMPLEMENTATION
  5. #include "stb_imag.h"
  6. #include "stb_imag_write.h"
  7. #include <cmath>
  8. using namespace std;
  9. class dynamicBitmap{
  10. private:
  11.     uint32_t *m;
  12.     uint32_t cols,rows;
  13. public:
  14.     dynamicBitmap(uint32_t r, uint32_t c,uint32_t v):rows(r),cols(c),m(new uint32_t [r*c]){
  15.         for(uint32_t i=0; i<r*c; i++){
  16.             m[i]=v;
  17.         }
  18.     }
  19.     ~dynamicBitmap(){
  20.         delete [] m;
  21.     }
  22.     dynamicBitmap(const dynamicBitmap& copy)
  23.             :rows(copy.rows),cols(copy.cols),m(new uint32_t[copy.rows*copy.cols]){
  24.         memcpy(m,copy.m,sizeof(uint32_t)*rows*cols);
  25.     }
  26.     /*
  27.     //old way
  28.     dynamicBitmap& operator=(const dynamicBitmap& orig){
  29.         if(this != &orig){
  30.             delete[] m;
  31.             rows=orig.rows;
  32.             cols=orig.cols;
  33.             m= new uint32_t[rows*cols];
  34.             memcpy(m,orig.m, sizeof(uint32_t)*rows*cols);
  35.             return *this;
  36.         }
  37.     }
  38.     */

  39.     /*new way*/
  40.     dynamicBitmap& operator=(dynamicBitmap orig){
  41.         cols=orig.cols;
  42.         rows=orig.rows;
  43.         swap(m,orig.m);
  44.         //memcpy(m,orig.m, sizeof(uint32_t)*rows*cols);
  45.         return *this;
  46.     }

  47.     /*move constructor*/
  48.     dynamicBitmap(dynamicBitmap&& orig):rows(orig.rows),cols(orig.cols),m(orig.m){
  49.         orig.m=nullptr;
  50.     }

  51.     void horizLine(uint32_t x1, uint32_t x2, uint32_t y, uint32_t v){
  52.         for(uint32_t i=x1; i<=x2; i++){
  53.             m[y*cols+i]=v;
  54.         }
  55.     }
  56.     void vertLine(uint32_t y1, uint32_t y2, uint32_t x, uint32_t v){
  57.         for(uint32_t j=y1; j<=y2; j++){
  58.             m[j*cols+x]=v;
  59.         }
  60.     }
  61.     void drawRect(uint32_t x, uint32_t y,uint32_t w, uint32_t h, uint32_t v){
  62.         for(uint32_t i=x;i <=x+w; i++){
  63.             m[y*cols+i]=v;
  64.             m[(y+h)*cols+i]=v;
  65.         }
  66.         for(uint32_t j=y; j<=y+h; j++){
  67.             m[j*cols+x]=v;
  68.             m[j*cols+(x+w)]=v;
  69.         }
  70.     }

  71.     void fillRect(uint32_t x, uint32_t y,uint32_t w, uint32_t h, uint32_t v){
  72.         for(uint32_t i=x; i<=y+h; i++){
  73.             for(uint32_t j=y; j<= x+w; j++){
  74.                 m[i*j]=v;
  75.             }
  76.         }
  77.     }

  78.     void line(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t v){
  79.         int x, y, dx, dy, e;
  80.         dx = x2 - x1;
  81.         dy = y2 - y1;
  82.         e = -dx;
  83.         x = x1;
  84.         y = y1;
  85.         for (int i = 0; i <= dx; i++) {
  86.             m[y*x] = v;
  87.             x++;
  88.             e = e + 2 * dy;
  89.             if (e >= 0) {
  90.                 y++;
  91.                 e = e - 2 * dx;
  92.             }
  93.         }
  94.     }

  95.     void ellipse(uint32_t xcenter, uint32_t ycenter, uint32_t xdiameter, uint32_t ydiameter, uint32_t v){
  96.             uint32_t X;
  97.             uint32_t Y;
  98.             uint32_t  a = xdiameter / 2;
  99.             uint32_t b = ydiameter / 2;
  100.             const double PI = 3.14;
  101.             double del = 0.001;
  102.             for (double i = -PI; i < PI; i = i + del) {
  103.                 Y = ycenter + b * sin(i);
  104.                 X = xcenter + a * cos(i);
  105.                 m[Y*rows+X] = v;
  106.             }
  107.     }
  108.     void save(const char filename[]) {
  109.         stbi_write_png(filename, rows, cols, 4, m, rows*cols * 4);
  110.     }


  111. };


  112. int main() {
  113.     // FIRST PART (ADD COLORS HERE)
  114.     constexpr uint32_t BLACK = 0xFF000000; // black opaque
  115.     constexpr uint32_t RED = 0xFF0000FF; // red opaque
  116.     constexpr uint32_t BLUE = 0xFFFF0000; // blue opaque
  117.     constexpr uint32_t WHITE = 0xFFFFFFFF; // white opaque
  118.     constexpr uint32_t YELLOW = 0xFF00FFFF; // yellow opaque
  119.     constexpr uint32_t GREEN = 0xFF00FF00; // green opaque

  120.     // SECOND PART (DO NOT EDIT)
  121.     int xcenter = 100;
  122.     int ycenter = 100;
  123.     int xdiameter = 200;
  124.     int ydiameter = 100;

  125.     dynamicBitmap b(1024, 1024, BLACK); // Potentially dynamic size (Now: 1024 x 1024 pixels)

  126.     b.horizLine(0, 500, 200, RED); // horizontal line from x=0 to x=500, @y = 200
  127.     b.vertLine(0, 399, 300, RED); // y=0 to y=399 @ x= 300, red vertical line
  128.     b.drawRect(200,200, 100,50, BLUE); // blue rectangle, top-left=200,200 w=100 h=50
  129.     b.fillRect(201,201, 98,48, WHITE); // same but filled
  130.     b.line(400,0, 550,300, YELLOW); // draw a line https://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  131.     b.ellipse(xcenter, ycenter, xdiameter, ydiameter,YELLOW);
  132.     b.save("bitmap.png");

  133. }
  134. 第二种方法用dynamicbitmap 来做然后我写了,编译也没错,但是为啥出不了png的图呀,请问你可以帮我看看哪里有问题吗?很奇怪,还有我在draw 和fillrect的时候感觉行和列有点问题,你可以帮我看一下吗,谢谢!
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-7 15:14:28 | 显示全部楼层    本楼为最佳答案   
本帖最后由 superbe 于 2019-11-7 15:18 编辑

一些修改

  1.     void fillRect(uint32_t x, uint32_t y,uint32_t w, uint32_t h, uint32_t v){
  2.         /*for(uint32_t i=x; i<=y+h; i++){
  3.             for(uint32_t j=y; j<= x+w; j++){
  4.                 m[i*j]=v;
  5.             }
  6.         }*/
  7.         for(uint32_t i = x; i <= x + w; i++)
  8.             for(uint32_t j = y; j <= y + h; j++){
  9.                 m[i + j * cols] = v;
  10.             }
  11.         
  12.     }

  13.     /* 这个函数有问题,画出的线位置不对,两个程序的line代码都改掉。参考Bresenham算法链接里的伪代码修改吧。
  14.        或参考我上面程序的line代码(也是照链接改的)。吴小林算法链接里也有伪代码,也照着编写吧。 */
  15.     void line(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t v){
  16.           。。。
  17.     }

  18.     void ellipse(uint32_t xcenter, uint32_t ycenter, uint32_t xdiameter, uint32_t ydiameter, uint32_t v){
  19.             uint32_t X;
  20.             uint32_t Y;
  21.             uint32_t  a = xdiameter / 2;
  22.             uint32_t b = ydiameter / 2;
  23.             const double PI = 3.14;
  24.             double del = 0.001;
  25.             for (double i = -PI; i < PI; i = i + del) {
  26.                 Y = ycenter + b * sin(i);
  27.                 X = xcenter + a * cos(i);
  28.                 //m[Y*rows+X] = v;  //这行不对,改成下行
  29.                 m[Y * cols + X] = v;
  30.             }
  31.     }
  32.     void save(const char filename[]) {
  33.         //stbi_write_png(filename, rows, cols, 4, m, rows*cols * 4);
  34.         stbi_write_png(filename, cols, rows, 4, m, cols * 4);
  35.     }
复制代码


修改后运行测试,只有line画线不正确。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-11-9 20:46:56 | 显示全部楼层
第二个程序的完整代码,仅供参考
main.rar (2.49 KB, 下载次数: 0)

其中changeColor函数我只是简单的把r g b每个分量都同等的乘以亮度(0<=brightness<=1),应该有更合理的方法。
但测试antialiasedLine的效果还是可以的,你多画几条线放大看有边缘虚化的效果。

有的代码是参考网上的,我也看不太懂。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-10 22:31:16 | 显示全部楼层
superbe 发表于 2019-11-9 20:46
第二个程序的完整代码,仅供参考


好的谢谢啦!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-3 18:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表