画圆(使用应用于带有for循环的图像中的像素) [英] Draw circle (using pixels applied in an image with for loop)

查看:0
本文介绍了画圆(使用应用于带有for循环的图像中的像素)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用像素位置(从左上角开始,右下角结束)画一个圆(1或2表示循环)

我使用此方法成功绘制了一个矩形:

private void drawrect(int width,int height,int x,int y) {
    int top=y;
    int left=x;

    if(top<0){
        height+=top;
        top=0;
        }
    if(left<0){
        width+=left;
        left=0;
    }

    for (int j = 0; j <width; j++) {
        for (int i = 0; i <height; i++) {
                    pixels[((i+top)*w)+j+left] = 0xffffff;//white color
        }

    }

}

像素数组包含像素索引,后跟其颜色。

pixels[index]=color;

在此之前,我将此代码用于"图像"和"像素"数组(如果这对您有帮助的话)

img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();

但我如何才能像此图像一样只绘制白色像素而忽略其他像素?

推荐答案

以下是使用像素绘制圆的代码:它使用公式xend=x+r cos(角度)和yend=y+r sin(角度)。

#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <math.h>

void DrawCircle(int x, int y, int r, int color)
{
      static const double PI = 3.1415926535;
      double i, angle, x1, y1;

      for(i = 0; i < 360; i += 0.1)
      {
            angle = i;
            x1 = r * cos(angle * PI / 180);
            y1 = r * sin(angle * PI / 180);
            putpixel(x + x1, y + y1, color);
      }
}

引用:http://www.softwareandfinance.com/Turbo_C/DrawCircle.html

这篇关于画圆(使用应用于带有for循环的图像中的像素)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆