直线段扫描转换算法——Bresenham算法C++实现

直线段扫描转换算法——Bresenham算法简介以及C++实现

目录

C++实现

参考:《计算机图形学 基础教程 第二版 》孙家广、胡事民 编著

#include<math.h>

struct CellUsignPoint_2d  
{
unsigned int x;
unsigned int y;
};
//由bresenham_2d()提取的直线上点将保存在vector中
void bresenham_2d(std::vector<CellUsignPoint_2d> &cells, int x0, int y0, int x1, int y1)
{
  int dx = x1 - x0;
  int dy = y1 - y0;
  unsigned int abs_dx = abs(dx);
  unsigned int abs_dy = abs(dy);
  int offset_dx = dx > 0 ? 1.0 : -1.0;
  int offset_dy = dy > 0 ? 1.0 : -1.0;
  CellUsignPoint_2d tmp_cell;
  tmp_cell.x = x0;
  tmp_cell.y = y0;
  cells.push_back(tmp_cell);
  // if x is dominant
  if (abs_dx >= abs_dy)
  {
    int error = -abs_dx;
    for (size_t i = 0; i < abs_dx; i++)
    {
      tmp_cell.x += offset_dx;
      error += 2*abs_dy;
      if (error > 0)
      {
        tmp_cell.y += offset_dy;
        error -= 2*abs_dx;
      }
      cells.push_back(tmp_cell);
    }
    return;
  }
  // if y is dominant
  else
  {
    int error = -abs_dy;
    for (size_t i = 0; i < abs_dy; i++)
    {
      tmp_cell.y += offset_dy;
      error += 2*abs_dx;
      if (error > 0)
      {
        tmp_cell.x += offset_dx;
        error -= 2*abs_dy;
      }
      cells.push_back(tmp_cell);
    }
    return;
  }
}

简介待续···

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值