- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
使用 C 编程和 OpenGL 的 Mandelbrot 集分形。这是我的代码。它现在只在中心显示一个点。我无法弄清楚我哪里出错了。我很确定我的数学是正确的。也许我在错误的循环中有什么?
This picture is what Im trying to get
到目前为止,这是我的代码:
#include <GLUT/glut.h>
#include <math.h>
void init(void);
void display(void);
const int screenWidth = 640;
const int screenHeight = 480;
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(0, 0);
glutCreateWindow("Mandelbrot");
// glViewport(-320, -320, 320, 320);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init(void) {
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-500.0, screenWidth, -500.0, screenHeight);
// A = screenWidth / 4.0;
// B = 0.0;
// C = D = screenHeight / 2.0;
}
void display(void) {
GLdouble x, f, y, xtemp, y0, x0, iteration, maxInteration;
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1);
glColor3f(0.0, 0.0, 0.0);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
for (y0 = - 1; y0 < 1.1; y0 = y0 + 0.0025) {
for (x0 = -2.5; x0 < 1.1; x0 = x0 + 0.0025) {
x = 0;
y = 0;
iteration = 0;
maxInteration = 1000;
while (((x * x) + (y * y) < (2 * 2)) && iteration < maxInteration) {
xtemp = (x * x) - (y * y) + x0;
y = (2 * x * y) + y0;
x = xtemp;
iteration = iteration + 1;
if (y <= 2) {
glBegin(GL_POINTS);
glVertex2d(x / 750, y / 750);
glEnd();
}
}
}
}
glFlush();
}
这是我在修改评论中的建议后更新的代码。它产生了上图。但是,现在我正在尝试在对象周围创建灰色圆圈???我试图在最后通过 else 来做到这一点……有什么想法吗?
#include <GLUT/glut.h>
#include <math.h>
void init(void);
void display(void);
const int screenWidth = 640;
const int screenHeight = 640;
GLdouble A, B, C, D;
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(0, 0);
glutCreateWindow("Mandelbrot");
glViewport(-1, 1, -1, 1);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init(void) {
//glMatrixMode(GL_PROJECTION);
gluOrtho2D(-3.0, 3.0, -3.0, 3.0);
A = screenWidth / 4.0;
B = 0.0;
C = D = screenHeight / 2.0;
}
void display(void)
{
GLdouble x, f, y, xtemp, y0, x0, iteration, maxInteration;
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
for(y0 = -1; y0< 1.1; y0 = y0 + 0.0025){
for (x0 = -2.5; x0 < 1.1; x0 = x0 + 0.0025) {
x = 0;
y = 0;
iteration = 0;
maxInteration = 200;
while(((x*x) + (y*y) <(2*2)) && iteration <maxInteration){
xtemp = (x*x) - (y*y) + x0;
y = (2*x*y) +y0;
x = xtemp;
iteration = iteration + 1;
}
if(iteration >= maxInteration){
glBegin(GL_POINTS);
glVertex2d(x0 , y0);
glColor3f(0.0, 0.0, 0.0);
glEnd();
}
else{
????
}
}
}
glFlush();
}
最佳答案
首先,这里有一些关于您的代码的建议:
这是我的意思的一个小例子:
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
const int screen_width = 640;
const int screen_height = 480;
float c[4];
float z[4];
float clamp(float x, float vmin, float vmax) {
if (x < vmin) {
return vmin;
} else if (x > vmax) {
return vmax;
}
return x;
}
void dc_add(float *a, float *b, float *res) {
res[0] = a[0] + b[0];
res[1] = a[1] + b[1];
res[2] = a[2] + b[2];
res[3] = a[3] + b[3];
}
void dc_mul(float *a, float *b, float *res) {
res[0] = a[0] * b[0] - a[1] * b[1];
res[1] = a[0] * b[1] + a[1] * b[0];
res[2] = a[0] * b[2] + a[2] * b[0] - a[1] * b[3] - a[3] * b[1];
res[3] = a[0] * b[3] + a[3] * b[0] + a[2] * b[1] + a[1] * b[2];
}
void dc_sqr(float *a, float *res) {
res[0] = a[0] * a[0] - a[1] * a[1];
res[1] = 2.0f * a[0] * a[1];
res[2] = 2.0f * (a[0] * a[2] - a[1] * a[3]);
res[3] = 2.0f * (a[0] * a[3] + a[1] * a[2]);
}
float dot(float x, float y) { return x * x + y * y; }
void init(void) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, screen_width, 0, screen_height);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
for (int y = 0; y < screen_height; y++) {
for (int x = 0; x < screen_width; x++) {
float px = -1.0f + 2.0f * (float)x / (float)screen_width;
float py = -1.0f + 2.0f * (float)y / (float)screen_height;
px *= (float)screen_width / (float)screen_height;
float tz = 0.5f;
float zo = powf(1.2f, 1.2f);
float m2 = 0.0f;
float co = 0.0f;
float temp[4];
c[0] = px * zo; c[1] = py * zo; c[2] = 1.0; c[3] = 0.0;
z[0] = 0.0f; z[1] = 0.0f; z[2] = 0.0f; z[3] = 0.0f;
for (int i = 0; i < 256; i++) {
if (m2 > 1024.0f) continue;
dc_sqr(z, temp);
dc_add(temp, c, z);
m2 = dot(z[0], z[1]);
co += 1.0f;
}
float d = 0.0f;
if (co < 256.0f) {
d = sqrtf((dot(z[0], z[1]) / dot(z[2], z[3]))) *
logf(dot(z[0], z[1]));
}
d = clamp(4.0f * d / zo, 0.0f, 1.0f);
d = powf(d, 0.25f);
glColor3f(d, d, d);
glBegin(GL_POINTS);
glVertex2d(x, y);
glEnd();
}
}
glFlush();
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(screen_width, screen_height);
glutInitWindowPosition(0, 0);
glutCreateWindow("Mandelbrot");
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
return 0;
}
这是输出:
上述例子的数学是基于this着色玩具。
上面的代码非常低效且缓慢,但它的主要目的是向您证明您永远不应该编写正确的 mandelbrot。
快乐编码。
关于c - 未显示 Mandelbrot 集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44187454/
我现在在 Mandelbrot 集的 DirectX 11 版本上工作了几天。到目前为止,我所做的是创建一个带有纹理的四边形。我可以使用 Pixel Shader 为点着色,但由于某种原因,Pixel
我在 Javascript 上编写了一个程序,用于创建 mandelbrot 分形,并将其绘制在 html Canvas 中。我的渲染方法是每行迭代,从 0 到 500 像素,然后简单地执行一个循环,
我的类(class)布置了一项作业,要求我编写一个程序来绘制曼德尔布洛特图形。 我们必须基本上让程序绘制结果的位图。 问题是,我的 CalcMBF 函数只输出 2 作为 Mandelbrot 数。 我
我认为问题在于我如何将笛卡尔坐标转换为复数,但我现在知道如何操作了。你能解释一下我应该如何转换吗?这是我尝试过的: double c_Im = (y + (maxIm - minIm)) / heig
我是 C++ 编程的新手,为了改进,我正在尝试制作一个 mandelbrot set consol 应用程序。我已经让它几乎完美地工作:图像生成,我可以放大/缩小,并且非常容易地四处移动。不过,我遇到
我想使用 Java 生成 Mandelbrot 集的 PNG 照片,输出应该可以在 Google 图片搜索中轻松找到。 该集合定义为以下序列: z_n+1 = z_n^2 + c 其中 c 和 z 是
我用 Java 编写了一个 Mandelbrot 集分形,并包含了在一定程度上平移和放大分形的功能。唯一的问题是,当我平移图像并尝试放大时,它看起来好像试图放大中心并平移一点。平移和缩放并不是真正的平
我用 python 编写了 Mandelbrot 集,但它看起来很奇怪,所以我搜索了平滑的颜色。我已经使用对数和线性插值编写了一个平滑的着色函数,但无论我尝试什么,我都无法得到我想要的: self.p
我编写了一个简单的片段着色器来渲染 mandelbrot 集。我正在使用 c 语言和使用 glsl 的 opengl 执行此操作。 #version 330 core in vec2 fCoord;
我正在尝试制作一个程序,通过制作一个 .PPM 文件来生成标准 Mandelbrot 集的图像。该程序没有生成有效的 PPM 文件,我不知道为什么。 这是我的代码: #include #includ
我知道已经回答了很多关于此的问题。然而,我的略有不同。每当我们实现我所理解的平滑着色算法时。 mu = 1 + n + math.log2(math.log2(z)) / math.log2(2)
mandelbrot 集包含 mandelbrot 迭代有界的点,迭代点永远不会“逃逸”。 让我们将边界定义为点,其中迭代点在 N 次迭代后逃逸(逃逸我的意思是与原点的距离变得大于 2)。 是否可以保
我一直在做 Mandelbrot 集并尝试缩放,但缩放模式变得非常麻烦。当我缩放时,它会完美缩放,但图像尺寸会缩小到原始尺寸的一半。下次我再次缩放时,图片尺寸会增加并尝试跳过查看窗口。代码在 c++/
我目前正在编写一个程序来生成非常巨大的(65536x65536 像素及以上)Mandelbrot 图像,并且我想设计一个光谱和着色方案来使它们公平。 wikipedia featured mandel
谁能解释一下扰动是如何描述的in this paper加速渲染 Mandelbrot 集? 我知道如何使用对每个像素执行多次迭代的传统方法来渲染 Mandelbrot 集,但我不太明白那篇论文中描述的
这是我尝试使用 Pygame 模块在 Python 3.5 中编写 Mandelbrot 集。 import math, pygame pygame.init() def mapMandelbrot(
我正在使用 C 编写以下代码。到目前为止,它一直在工作,并且已缩放到正确的级别等,但是我正在努力让颜色按我想要的方式工作。理想情况下,无论颜色如何,我都希望得到这样的结果: 但是我的程序如下所示,目前
我可以生成从 minReal 到 maxReal 以及从 minImaginary 到 maxImaginary 的 Mandelbrot 集的 400x400 图像。所以, makeMandel(m
我正在编写绘制 Mandelbrot 集版本的代码。当它运行时,它接受两个输入 a 和 b,并将其转换为一个复数(例如 complex(a,b))。然后它绘制一个 Mandelbrot 集,其中 z
尝试使用与我在使用 TBB(线程构建块)运行时使用的代码相同的代码(有点)。 我对 OpenCL 没有太多经验,但我认为大部分主要代码是正确的。我相信错误在 .cl文件,它在那里进行数学运算。 这是我
我是一名优秀的程序员,十分优秀!