c_cpp 输出像素化的圆圈到控制台。对图形或Minecraft有用。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 输出像素化的圆圈到控制台。对图形或Minecraft有用。相关的知识,希望对你有一定的参考价值。
#include <iostream>
#include <vector>
#include <array>
typedef std::array<float, 2> xyCoord;
float isInCircle(float x, float y, float radius);
void setSquareCoords(int row, int column, std::array<xyCoord, 4>& square);
int main()
{
enum {x, y};
float radius;
int dia;
std::vector<std::vector<bool>> squares;
std::array<xyCoord, 4> squareCorners; // coords for square's corners
squareCorners.fill({{0.0f, 0.0f}});
// Input diameter
std::cout << "Diameter: ";
std::cin >> dia;
std::cin.ignore();
std::cout << std::endl;
squares.resize(dia);
radius = static_cast<float>(dia) / 2.0f;
// Generate circle by looping through each row / col position
for (int row = 0; row < dia; row++)
{
for (int col = 0; col < dia; col++)
{
int sum = 0;
setSquareCoords(row, col, squareCorners);
// If 2/4 corners of block are inside circle radius, block is inside circles
for (auto corner : squareCorners)
{
if (isInCircle(corner[x], corner[y], radius))
sum++;
}
if (sum >= 2)
squares[row].push_back(true);
else
squares[row].push_back(false);
}
}
// output to console
for (auto row : squares)
{
for (auto position : row)
{
if (position == true)
std::cout << '#';
else
std::cout << ' ';
}
std::cout << std::endl;
}
std::cin.ignore();
return 0;
}
float isInCircle(float x, float y, float radius)
{
x -= radius;
y -= radius;
return (x * x + y * y <= radius * radius);
}
void setSquareCoords(int row, int column, std::array<xyCoord, 4>& square)
{
enum {top_left, top_right, bottom_left, bottom_right};
enum {x, y};
for (unsigned corner = 0; corner < square.size(); corner++)
{
switch (corner)
{
case top_left:
square[corner][x] = static_cast<float>(row);
square[corner][y] = static_cast<float>(column);
break;
case top_right:
square[corner][x] = static_cast<float>(row);
square[corner][y] = static_cast<float>(column) + 1.0f;
break;
case bottom_left:
square[corner][x] = static_cast<float>(row) + 1.0f;
square[corner][y] = static_cast<float>(column);
break;
case bottom_right:
square[corner][x] = static_cast<float>(row) + 1.0f;
square[corner][y] = static_cast<float>(column) + 1.0f;
break;
}
}
}
以上是关于c_cpp 输出像素化的圆圈到控制台。对图形或Minecraft有用。的主要内容,如果未能解决你的问题,请参考以下文章
图形转动锐利/像素化的原因
计算机图形学——直线扫描转换
Python以精确的像素大小保存matplotlib图形
光栅化插值方法
计算机图形学输出图元_14_OpenGL像素阵列函数_1_位图函数
计算机图形学输出图元_3_画线算法_1_直线方程