鱼C论坛

 找回密码
 立即注册
查看: 4395|回复: 14

关于希尔伯特曲线

[复制链接]
发表于 2023-1-31 08:45:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
看到B站有一位Up用希尔伯特曲线做降维打击
手痒也想试试看
可是怎么把线拉直……(没错,这问题考虑了我好几天)
有人知道吗

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-2-6 21:51:50 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-1-31 08:53:13 | 显示全部楼层
还有三维的希尔伯特曲线如何构造视频里没说
二维希尔伯特已经成功做出来
代码如下
  1. import pygame

  2. def init():
  3.     global origin
  4.     origin = pygame.Surface((3, 3))
  5.     origin.fill((255, 255, 255))
  6.     points = [
  7.         (0, 0), (1, 0), (2, 0),
  8.         (0, 1),         (2, 1),
  9.         (0, 2),         (2, 2)
  10.     ]
  11.     for point in points:
  12.         origin.set_at(point, (0, 0, 0))

  13. def plot():
  14.     newS = newW, newH = (originW * 2 + 1, originH * 2 + 1)
  15.     w1 = newW // 2
  16.     h1 = newH // 2
  17.     originR90 = pygame.transform.rotate(origin, -90)
  18.     originL90 = pygame.transform.rotate(origin, 90)
  19.     newer = pygame.transform.scale(none, newS)
  20.     newer.blit(origin, (0, 0))
  21.     newer.blit(origin, (w1 + 1, 0))
  22.     newer.blit(originR90, (0, h1 + 1))
  23.     newer.blit(originL90, (w1 + 1, h1 + 1))
  24.     newer.set_at((0, h1), (0, 0, 0))
  25.     newer.set_at((w1, h1 - 1), (0, 0, 0))
  26.     newer.set_at((newW - 1, h1), (0, 0, 0))
  27.     return newer

  28. def main():
  29.     global originS, originW, originH
  30.     global screen, origin
  31.     pygame.init()
  32.     screen = pygame.display.set_mode(size)
  33.     init()
  34.     plots = [origin]
  35.     for i in range(6):
  36.         origin = plot()
  37.         originS = originW, originH = origin.get_size()
  38.         plots.append(origin)
  39.     plots.append(plot())
  40.     clock = pygame.time.Clock()
  41.     counter = 0
  42.     running = True
  43.     while running:
  44.         clock.tick(1)
  45.         screen.fill((255, 255, 255))
  46.         screen.blit(pygame.transform.scale(plots[
  47.             counter
  48.         ], size), (0, 0))
  49.         for event in pygame.event.get():
  50.             if event.type == pygame.QUIT:
  51.                 running = False
  52.         pygame.display.flip()
  53.         counter += 1
  54.         counter %= len(plots)
  55.     pygame.quit()

  56. size = width, height = (600, 600)
  57. screen = None
  58. origin = None
  59. originS = originW, originH = (3, 3)
  60. none = pygame.Surface(originS)
  61. none.fill((255, 255, 255))

  62. if __name__ == '__main__':
  63.     main()
复制代码

注:二维希尔伯特曲线代码随便拿去用吧,反正求助的时候代码还是要展示出来的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-2-16 07:34:12 | 显示全部楼层
没人啊…
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

 楼主| 发表于 2023-4-2 14:59:25 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-4-2 15:22:39 | 显示全部楼层
以前有款手机游戏名字叫【贪食蛇】
【希尔伯特曲线】应该是它的极致解法吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-6 10:19:47 | 显示全部楼层
蹲答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-6 19:10:53 | 显示全部楼层
碳基生物脑子炸了,硅基生物来了:
请尝逝一下这段AI生成的代码:
  1. import matplotlib.pyplot as plt
  2. from mpl_toolkits.mplot3d import Axes3D
  3. from hilbertcurve.hilbertcurve import HilbertCurve

  4. # 设置希尔伯特曲线的参数
  5. p = 3  # order
  6. n = 3  # dimension
  7. hilbert_curve = HilbertCurve(p, n)

  8. # 获取曲线上点的总数
  9. max_distance = 2**(p * n) - 1

  10. # 将希尔伯特曲线转为三维坐标系下的坐标点
  11. points = []
  12. for i in range(max_distance + 1):
  13.     point = hilbert_curve.point_from_distance(i)
  14.     points.append(point)

  15. # 提取出 x, y, z 坐标
  16. x = [p[0] for p in points]
  17. y = [p[1] for p in points]
  18. z = [p[2] for p in points]

  19. fig = plt.figure()
  20. ax = fig.add_subplot(111, projection='3d')

  21. # 画出希尔伯特曲线
  22. ax.plot(x, y, z)

  23. # 设定图形显示的角度
  24. ax.view_init(30, 45)

  25. # 显示图形
  26. plt.show()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-6 19:50:09 | 显示全部楼层
zhangchenyvn 发表于 2024-2-6 19:10
碳基生物脑子炸了,硅基生物来了:
请尝逝一下这段AI生成的代码:

把2#的代码也一起提问一下?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-6 19:55:47 | 显示全部楼层
本帖最后由 zhangchenyvn 于 2024-2-6 20:02 编辑

GPT4-turbo对此表示无能为力。。。
在这段代码中,加入三维希尔伯特曲线的功能会相当复杂,因为需要处理三维空间中的图形旋转和投影问题。由于Pygame库是专门为二维图形设计的,且这段代码并没有提供一个直接的三维绘制环境,要实现这一功能,就需要自己实现一个三维到二维的投影算法,同时还需要设计三维希尔伯特曲线的生成算法。
一种可能的方法是,你可以为每个旋转增加额外的维度处理,然后写一个简单的3D渲染器来处理这些旋转,并将其投影到2D平面上。这将涉及到一些数学处理,比如使用矩阵变换等。
但这将是一个非常大的任务,并且远远超出了这一简单的对话交互所能处理的范围。为了真正实现这个功能,你可能需要寻找专门处理三维图形的Python库(如PyOpenGL),并做大量的数学和图形编程工作。
如果你仍然希望在现有的Pygame框架内尝试简单地模拟三维希尔伯特曲线,你可能需要查阅有关三维希尔伯特曲线的算法文献,以及关于三维图形的基础知识,然后以此为基础扩展你的代码。或者,你可以尝试创建一个“伪三维”视觉效果,通过二维模拟来创造三维效果的错觉。但请注意,这依然是一个复杂的任务,需要扎实的编程和数学技能。

我强行让他编写代码,他输出了:
  1. import matplotlib.pyplot as plt
  2. from mpl_toolkits.mplot3d import Axes3D

  3. def hilbert_curve(ax, center, size, level, head, up, right):
  4.     if level == 0:
  5.         ax.scatter(center[0], center[1], center[2], color='black')
  6.     else:
  7.         half = size / 2
  8.         step1 = [up[i] - right[i] - head[i] for i in range(3)]
  9.         step2 = [up[i] + right[i] - head[i] for i in range(3)]
  10.         step3 = [up[i] + right[i] + head[i] for i in range(3)]
  11.         step4 = [up[i] - right[i] + head[i] for i in range(3)]

  12.         third = size / 3
  13.         hilbert_curve(ax, [center[i] - step1[i] * third for i in range(3)], half, level-1, up, right, head)
  14.         hilbert_curve(ax, [center[i] - step2[i] * third for i in range(3)], half, level-1, head, up, right)
  15.         hilbert_curve(ax, [center[i] - step3[i] * third for i in range(3)], half, level-1, head, up, right)
  16.         hilbert_curve(ax, [center[i] + step4[i] * third for i in range(3)], half, level-1, up, right, head)
  17.         hilbert_curve(ax, [center[i] + step1[i] * third for i in range(3)], half, level-1, up, right, head)
  18.         hilbert_curve(ax, [center[i] + step2[i] * third for i in range(3)], half, level-1, head, up, right)
  19.         hilbert_curve(ax, [center[i] + step3[i] * third for i in range(3)], half, level-1, head, up, right)
  20.         hilbert_curve(ax, [center[i] - step4[i] * third for i in range(3)], half, level-1, up, right, head)

  21. # 建立图表,轴和3D Hilbert曲线
  22. fig = plt.figure()
  23. ax = fig.add_subplot(111, projection='3d')

  24. # 设置三维希尔伯特曲线的初始参数
  25. center = [0, 0, 0]
  26. size = 100
  27. level = 3  # 曲线的递归深度
  28. head = [1, 0, 0]  # 曲线朝向
  29. up = [0, 0, 1]  # 曲线定义的“上”方向
  30. right = [0, 1, 0]  # 曲线定义的“右”方向

  31. # 递归生成3D Hilbert曲线
  32. hilbert_curve(ax, center, size, level, head, up, right)

  33. # 调整图标尺寸和角度
  34. ax.set_xlim(-size, size)
  35. ax.set_ylim(-size, size)
  36. ax.set_zlim(-size, size)

  37. # 展示图标
  38. plt.show()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-6 21:18:29 From FishC Mobile | 显示全部楼层
视频发一下链接?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-7 10:50:36 | 显示全部楼层
看完了视频,我差点吐了。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-7 12:57:40 | 显示全部楼层
  1. #!/usr/bin/env python
  2. from manimlib.imports import *


  3. class CurveDemo(Scene):
  4.     """docstring for CurveDemo"""

  5.     def construct(self):
  6.         curve = HilbertCurve3D()
  7.         self.play(ShowCreation(curve), run_time=20)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-7 13:10:53 | 显示全部楼层
本帖最后由 zhangchenyvn 于 2024-2-7 13:19 编辑

题主,我在某帖子上,看到说这个项目在GitHub上开源了。
开源地址
DimReduce-master.zip (1.19 MB, 下载次数: 2)
食用方法:详见readme.md
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-8 00:43:08 | 显示全部楼层
歌者文明清理员 发表于 2023-1-31 08:53
还有三维的希尔伯特曲线如何构造视频里没说
二维希尔伯特已经成功做出来
代码如下

先这样:

W1ElBA4AAM.png

再这样:

f4gZ4LHHyV.png

关于希尔伯特曲线展开可以看下这个 -> https://www.bilibili.com/video/a ... 88.video.desc.click
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-8 17:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表