- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
出于学习目的,我正在编写自己的中点位移算法,我决定以自己的方式实现它,看看我是否 1) 能够理解该算法,以及 2) 看看我是否可以根据自己的喜好修改它。
这里是生成分形的代码:
public void Generate(Double rg, int size)
{
Random rand = new Random();
int min = -127;
int max = 128;
// Starting points of the rectangle
MDP_Point s1 = new MDP_Point(0, 0, rand.Next(min, max));
MDP_Point s2 = new MDP_Point(size, 0, rand.Next(min, max));
MDP_Point s3 = new MDP_Point(size, size, rand.Next(min, max));
MDP_Point s4 = new MDP_Point(0, size, rand.Next(min, max));
// Lists containing the rectangles
List<MDP_Rect> newRect = new List<MDP_Rect>(); // Newly created rectangles
List<MDP_Rect> oldRect = new List<MDP_Rect>(); // Rectangles being divided
// Starting rectangle is added to the list
oldRect.Add(new MDP_Rect(s1, s2, s3, s4));
// Distance between 2 points in a rectangle
int h = size;
while (h > 1)
{
foreach (MDP_Rect r in oldRect)
{
// Middle points of rectangle segments
MDP_Point m1 = new MDP_Point();
MDP_Point m2 = new MDP_Point();
MDP_Point m3 = new MDP_Point();
MDP_Point m4 = new MDP_Point();
// Middle point of rectangle
MDP_Point mm = new MDP_Point();
m1.x = (r.C1.x + r.C2.x) / 2;
m1.y = (r.C1.y + r.C2.y) / 2;
m1.z = ((r.C1.z + r.C2.z) / 2) +(rand.Next(min, max) * rg);
m2.x = (r.C2.x + r.C3.x) / 2;
m2.y = (r.C2.y + r.C3.y) / 2;
m2.z = ((r.C2.z + r.C3.z) / 2) +(rand.Next(min, max) * rg);
m3.x = (r.C3.x + r.C4.x) / 2;
m3.y = (r.C3.y + r.C4.y) / 2;
m3.z = ((r.C3.z + r.C4.z) / 2) +(rand.Next(min, max) * rg);
m4.x = (r.C1.x + r.C4.x) / 2;
m4.y = (r.C1.y + r.C4.y) / 2;
m4.z = ((r.C1.z + r.C4.z) / 2) + (rand.Next(min, max) * rg);
mm.x = (r.C1.x + r.C2.x + r.C3.x + r.C4.x) / 4;
mm.y = (r.C1.y + r.C2.y + r.C3.y + r.C4.y) / 4;
mm.z = ((r.C1.z + r.C2.z + r.C3.z + r.C4.z) / 4) + (rand.Next(min, max) * rg);
newRect.Add(new MDP_Rect(r.C1, m1, mm, m4));
newRect.Add(new MDP_Rect(m1, r.C2, m2, mm));
newRect.Add(new MDP_Rect(mm, m2, r.C3, m3));
newRect.Add(new MDP_Rect(m4, mm, m3, r.C4));
}
oldRect.Clear();
oldRect = new List<MDP_Rect>(newRect);
newRect.Clear();
h /= 2;
}
List<MDP_Rect> sorted = new List<MDP_Rect>();
sorted = oldRect.OrderBy(y => y.C1.y).ThenBy(x => x.C1.x).ToList();
List<MDP_Point> mapArray = new List<MDP_Point>();
mapArray.AddRange(CreateArray(sorted));
CreateImage(size, mapArray, rg);
}
MDP_Point 仅包含 x、y 和 z 值MDP_Rectangle 包含4个点,创建一个矩形
CreateArray() 方法仅采用有序的矩形列表并以正确的顺序输出和点列表以创建图像。
创建数组():
private List<MDP_Point> CreateArray(List<MDP_Rect> lRect)
{
List<MDP_Point> p = new List<MDP_Point>();
int size = (int)Math.Sqrt(lRect.Count);
int i = 0;
foreach (MDP_Rect r in lRect)
{
p.Add(new MDP_Point((int)r.C1.x, (int)r.C1.y, (int)r.C1.z));
if (i > 0 && i % size == size - 1)
{
p.Add(new MDP_Point((int)r.C2.x, (int)r.C2.y, (int)r.C2.z));
}
i++;
}
for (int a = 0; a < size; a++)
{
p.Add(new MDP_Point((int)lRect[(size * size - size) + a].C4.x,
(int)lRect[(size * size - size) + a].C4.y,
(int)lRect[(size * size - size) + a].C4.z));
if (a > 0 && a % size == size - 1)
{
p.Add(new MDP_Point((int)lRect[(size * size - size) + a].C3.x,
(int)lRect[(size * size - size) + a].C3.y,
(int)lRect[(size * size - size) + a].C3.z));
}
}
return p;
}
这是创建图像的方法:
private void CreateImage(int size, List<MDP_Point> arr, double roughness)
{
Bitmap map = new Bitmap(size, size);
int ver = 0;
for (int i = 0; i < map.Height; i++)
{
for (int n = 0; n < map.Width; n++ )
{
int h = (int)arr[ver].z + 127;
if (h < 0)
{
h = 0;
}
else if (h > 255)
{
h = 255 ;
}
Color c = Color.FromArgb(h, h, h);
//map.SetPixel(n, i, c);
map.SetPixel(i, n, c);
ver++;
}
}
Bitmap m = new Bitmap(map);
bool saved = true;
int num = 0;
while (saved)
{
if (File.Exists("map_S" + size + "_R" + roughness + "_" + num + ".png"))
{
num++;
}
else
{
m.Save("map_S" + size + "_R" + roughness + "_" + num + ".png", System.Drawing.Imaging.ImageFormat.Png);
saved = false;
}
}
map.Dispose();
m.Dispose();
}
任何低于 0 的值都设置为 0 而任何高于 255 的值都设置为 255 的事实可能是一个大问题....不知道该怎么做。
这是代码生成的图像:尺码:1024粗糙度:0.5
最明显的问题是对角“脊线”和那里的平铺外观。
在这一点上,我不确定如何解决这个问题以使其看起来更自然。有什么想法吗?
最佳答案
在这里,我认为部分问题是您使用 255 和 0 对 h
变量进行了黑客攻击。我尝试使用以下代码:
int h = (int) arr[ver].z;
if (h < 0)
{
h = Math.Abs(h);
}
while(h > 255)
{
h -= 255;
}
在我的电脑上,结果是:
当我使用时:
int h = (int) arr[ver].z + 127;
请注意,我只需要创建一个测试 MDP_Point
类和 MDP_Rect
来测试这个...
关于c# - 中点位移算法 - 奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11642248/
如果此答案为 Kotlin,我想转换代码:https://stackoverflow.com/a/5402769/2735398 我将其粘贴到 Intellij 中: private int deco
对于这种转变: System.out.println(0b10000000_00000000_00000000_00000001>>>32); 我得到这个输出值: -2147483647 对吗? 最佳
如果没有大量工作,我想要的可能是不可能的,但也许有人有解决方案。我有一个像下面这样的图(这当然是一个过于简单的例子),其中我的刻度标签彼此非常接近: dd <- data.frame(x=1:4, y
试过这个代码on Go playground : package main import ( "fmt" ) func main() { log2Dim := uint32(9)
我希望在用户滚动时获取 UIScrollView 的位移,每次滚动时只需要位移。使用这个方法 -(void)scrollViewWillEndDragging:(UIScrollView *)scro
我正在检查我在装有 iOs 5.1 的 Ipad 中的应用程序当我点击 textarea 时,光标会跳到书写线上方两行。虽然焦点位于正确的位置,但光标的视觉外观出现在焦点线上方两行。对此有任何建议。
你如何获得像 -10 这样的数字?从这些移位练习题中? 据我了解X*32可以写成 x> 1 = 0001 0111 >> 1 = 000 1011 = 11 //right bit-shift by
我正在查看适用于 Linux 的开源 AMD GPU 驱动程序。我注意到一些我以前没有见过的东西,我想知道目的。在sid.h文件的第1441行,有一系列的定义是整数被左移0位。这不就是对原始整数进行操
这个网站 localhotchat.com 有文字从红色栏流出。我试过调整它,但它似乎并没有消失。 主页(和内部)上的 14pt 文本片段应该在 red_colored_strip 内,但它不是,并且
问题#1 在 Java 中,移位是否比使用单个语句将 移位相同数的代价高出数倍? 例如,是 int x = 5; x = x << 16; 比快 int x = 5; for (int i=0; i<
我有三个 Div,周围的 Div 是“MainDiv”,里面的 Top Div 是“UpperDiv”下一个是“FooterDiv”。 在 Mozilla 中一切正常,但在 Chrome 中“Foot
所以,我正在阅读一本关于 Go 的书(Ivo Balbaert 的 The Way to Go),其中有一个代码示例: const hardEight = (1 > 97 因为我没有在这台机器上安装
Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。 下面就讲一下Tweene Animations。 主要类:
我有 UINavigationItem,我想向它添加 UISearchBar。我是这样做的: let searchBar = UISearchBar(frame: CGRectMake(0, 0, s
本文实例讲述了Android动画之渐变动画(Tween Animation)。分享给大家供大家参考,具体如下: Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的
我在 C# 中使用 openCV 库中的 Lucas Kanade 光流算法;有一系列帧,我想在每两个帧中找出什么是光流并将其显示在图片框中。 我可以从以下函数中获取 velX 和 velY: Emg
我是一名优秀的程序员,十分优秀!