魔方吧·中文魔方俱乐部

标题: 魔方模拟器twisty-sphere [打印本页]

作者: 怀表    时间: 2022-5-12 18:41:52     标题: 魔方模拟器twisty-sphere

本帖最后由 怀表 于 2022-5-12 23:32 编辑

https://github.com/worldmaker18349276/twisty-sphere
没有文档,我也搞不清楚是怎么自定义的。从例子看很炫酷。不过没边框很难看清块。

切割好像有两个方法sliceBySphere和sliceByPlane。不过球面切割生成魔方很慢,把resolution参数调小可提高速度。

调了半天弄出了个斜转,等明天再来试一下球面切割。
  1. cube = CSG.cube({center: [0, 0, 0], radius: [1.1, 1.1, 1.1]})
  2.   .intersect(CSG.cube({center: [ 0,-1, 0], radius: [2, 2, 2]}).setColor(1,1,1))
  3.   .intersect(CSG.cube({center: [ 0, 1, 0], radius: [2, 2, 2]}).setColor(1,1,0))
  4.   .intersect(CSG.cube({center: [-1, 0, 0], radius: [2, 2, 2]}).setColor(0,0,1))
  5.   .intersect(CSG.cube({center: [ 1, 0, 0], radius: [2, 2, 2]}).setColor(0,1,0))
  6.   .intersect(CSG.cube({center: [ 0, 0,-1], radius: [2, 2, 2]}).setColor(1,0,0))
  7.   .intersect(CSG.cube({center: [ 0, 0, 1], radius: [2, 2, 2]}).setColor(1,0.6,0.2));
  8. var puzzle = new ModeledSphPuzzle(cube);

  9. puzzle.sliceByPlane([1,1,1],0)
  10. puzzle.sliceByPlane([-1,1,1],0)
  11. puzzle.sliceByPlane([-1,-1,1],0)
  12. puzzle.sliceByPlane([-1,-1,-1],0)
  13. puzzle.sliceByPlane([-1,1,-1],0)
  14. puzzle.sliceByPlane([1,-1,1],0)
  15. puzzle.sliceByPlane([1,-1,-1],0)
  16. puzzle.sliceByPlane([1,1,-1],0)
  17. puzzle.clean();
  18. puzzle.prepare();

  19. var world = new ModeledSphPuzzleWorld(puzzle, "display");
复制代码
s1-fs8.png
  1. cube = CSG.cube({center: [0, 0, 0], radius: [1.1, 1.1, 1.1]})
  2.   .intersect(CSG.cube({center: [ 0,-1, 0], radius: [2, 2, 2]}).setColor(1,1,1))
  3.   .intersect(CSG.cube({center: [ 0, 1, 0], radius: [2, 2, 2]}).setColor(1,1,0))
  4.   .intersect(CSG.cube({center: [-1, 0, 0], radius: [2, 2, 2]}).setColor(0,0,1))
  5.   .intersect(CSG.cube({center: [ 1, 0, 0], radius: [2, 2, 2]}).setColor(0,1,0))
  6.   .intersect(CSG.cube({center: [ 0, 0,-1], radius: [2, 2, 2]}).setColor(1,0,0))
  7.   .intersect(CSG.cube({center: [ 0, 0, 1], radius: [2, 2, 2]}).setColor(1,0.6,0.2));
  8. var puzzle = new ModeledSphPuzzle(cube);


  9. puzzle.sliceByPlane([0,1,0],0)
  10. puzzle.sliceByPlane([1,0,1],0)
  11. puzzle.sliceByPlane([-1,0,1],0)
  12. puzzle.sliceByPlane([1,0,0],0)
  13. puzzle.sliceByPlane([0,0,1],0)

  14. puzzle.clean();
  15. puzzle.prepare();

  16. var world = new ModeledSphPuzzleWorld(puzzle, "display")
复制代码
s3-fs8.png
  1. var phi = (Math.sqrt(5) + 1)/2;

  2. function fzy_add(set, elem) {
  3.   set.add(fzy_snap(elem, set));
  4. }
  5. function csort(arr0) {
  6.   var arr_min = arr0.slice();
  7.   var arr_ = arr0.slice();

  8.   for ( let i=0,len=arr0.length; i<len; i++ ) {
  9.     if ( fzy_cmp(arr_, arr_min) < 0 )
  10.       arr_min = arr_.slice();
  11.     arr_.push(arr_.shift());
  12.   }
  13.   return arr_min;
  14. }

  15. var radius = (phi + 1)/Math.sqrt(phi + 2);
  16. var offset = 1;


  17. var faces = new Set();
  18. var vertices = [[1,1,1]];
  19. for ( let i=0; i<4; i++ )
  20.   vertices.push(rotate(vertices[vertices.length-1], [1/2,0,1/phi/2,phi/2]));
  21. faces.add(csort(vertices));
  22. for ( let face of faces ) {
  23.   fzy_add(faces, csort(face.map(v => rotate(v, [1/2,1/2,1/2,1/2]))));
  24.   fzy_add(faces, csort(face.map(v => rotate(v, [1/2,0,1/phi/2,phi/2]))));
  25. }

  26. var polygons = [];
  27. for ( let face of faces ) {
  28.   let polygon = new CSG.Polygon(face.map(v => new CSG.Vertex(new CSG.Vector3D(...v))));
  29.   let {x, y, z} = polygon.plane.normal;
  30.   let [r, g, b] = [x, y, z].map(v => Math.sign(v) * Math.ceil(Math.abs(v) - 0.01)).map(v => (v+1)/2);
  31.   polygon.setColor(r, g, b);
  32.   polygons.push(polygon);
  33. }
  34. var dodeca = CSG.fromPolygons(polygons);

  35. var puzzle = new ModeledSphPuzzle(dodeca, radius);

  36. puzzle.sliceByPlane([0,1,0],0)
  37. puzzle.sliceByPlane([1,0,1],0)
  38. puzzle.sliceByPlane([-1,0,1],0)
  39. puzzle.sliceByPlane([1,0,0],0)
  40. puzzle.sliceByPlane([0,0,1],0)

  41. puzzle.clean(1);
  42. puzzle.prepare();

  43. var world = new ModeledSphPuzzleWorld(puzzle, "display")
复制代码
s4-fs8.png
感觉跟胡波的正四面体切割群魔方java助手的用法有些像,不过定义多面体得手写,切割的面的法向量也得手写,调配色也很麻烦。

居然没有打乱功能。

附件: s1-fs8.png (2022-5-12 19:26:14, 96.32 KB) / 下载次数 58
http://bbs.mf8-china.com/forum.php?mod=attachment&aid=Mjc2OTEzfDY3ODBkYzA1fDE3MzM5NTgyODZ8MHww

附件: s3-fs8.png (2022-5-12 23:15:27, 61.45 KB) / 下载次数 56
http://bbs.mf8-china.com/forum.php?mod=attachment&aid=Mjc2OTE0fDJjMjU4ZmQ4fDE3MzM5NTgyODZ8MHww

附件: s4-fs8.png (2022-5-12 23:32:19, 76.78 KB) / 下载次数 54
http://bbs.mf8-china.com/forum.php?mod=attachment&aid=Mjc2OTE1fDUwZmFkOTQyfDE3MzM5NTgyODZ8MHww
作者: 怀表    时间: 2022-5-16 18:17:20

  1. cube = CSG.cube({center: [0, 0, 0], radius: [1.1, 1.1, 1.1]})
  2.   .intersect(CSG.cube({center: [ 0,-1, 0], radius: [2, 2, 2]}).setColor(1,1,1))
  3.   .intersect(CSG.cube({center: [ 0, 1, 0], radius: [2, 2, 2]}).setColor(1,1,0))
  4.   .intersect(CSG.cube({center: [-1, 0, 0], radius: [2, 2, 2]}).setColor(0,0,1))
  5.   .intersect(CSG.cube({center: [ 1, 0, 0], radius: [2, 2, 2]}).setColor(0,1,0))
  6.   .intersect(CSG.cube({center: [ 0, 0,-1], radius: [2, 2, 2]}).setColor(1,0,0))
  7.   .intersect(CSG.cube({center: [ 0, 0, 1], radius: [2, 2, 2]}).setColor(1,0.6,0.2));
  8. var puzzle = new ModeledSphPuzzle(cube);

  9. var g = 5/3
  10. var radius = Math.sqrt(29)/3
  11. puzzle.sliceBySphere([ g, 0, g], radius)
  12. puzzle.sliceBySphere([ -g,0, g], radius)
  13. puzzle.sliceBySphere([ g,0,-g], radius)
  14. puzzle.sliceBySphere([ -g,0,-g], radius)
  15. puzzle.sliceByPlane([0,1,0],0)
  16. puzzle.sliceByPlane([1,0,1],0)
  17. puzzle.sliceByPlane([-1,0,1],0)

  18. puzzle.clean();
  19. puzzle.prepare();

  20. var world = new ModeledSphPuzzleWorld(puzzle, "display");
复制代码
s5-fs8.png

附件: s5-fs8.png (2022-5-16 18:17:04, 103.23 KB) / 下载次数 57
http://bbs.mf8-china.com/forum.php?mod=attachment&aid=Mjc2OTE4fDZmM2YzZjZifDE3MzM5NTgyODZ8MHww




欢迎光临 魔方吧·中文魔方俱乐部 (http://bbs.mf8-china.com/) Powered by Discuz! X2