在子程序中进行变换不起作用(绘制时)

编程语言 2026-07-11

我正在尝试定义一个子程序,用来绘制一个球体,供在绘制流程中使用。

sphere(radius,coords) := block([t],
    transform = [x+coords[1] , y+coords[2] , z+coords[3], x, y, z], 
    spherical(radius,a,0,2*%pi,z1,0,%pi)   
)$

也许已经有一个实现该功能的例程,若有请随时告诉我——但我仍在问,我应该怎么做才能让变换起作用。

load(draw);
r1: 3$  /* Radius of sphere 1 */
c1: [10,10,10]$ /* Center of sphere 1 */
r2: 4$  /* Radius of sphere 2 */
c2: [5,5,5]$ /* Center of sphere 2 */

sphere(radius,coords) := block([t],
    transform = [x+coords[1] , y+coords[2] , z+coords[3], x, y, z], 
    spherical(radius,a,0,2*%pi,z1,0,%pi)   
)$
draw3d(
        color     = blue,    opacity   = 0.1,
 /* transform = [x+c1[1] , y+c1[2] , z+c1[3], x, y, z],  */
        sphere(r1,c1)   ,

    color     = orange,    opacity   = 0.1,
/*  transform = [x+c2[1] , y+c2[2] , z+c2[3], x, y, z],  */
        sphere(r2,c2)   
)$

输出

以 0,0,0 为中心的球体未在变换后的坐标处

load(draw);
r1: 3$  /* Radius of sphere 1 */
c1: [10,10,10]$ /* Center of sphere 1 */
r2: 4$  /* Radius of sphere 2 */
c2: [5,5,5]$ /* Center of sphere 2 */

sphere(radius,coords) := block([t],
    transform = [x+coords[1] , y+coords[2] , z+coords[3], x, y, z], 
    spherical(radius,a,0,2*%pi,z1,0,%pi)   
)$
draw3d(
        color     = blue,    opacity   = 0.1,
 transform = [x+c1[1] , y+c1[2] , z+c1[3], x, y, z], 
        sphere(r1,c1)   ,

    color     = orange,    opacity   = 0.1,
transform = [x+c2[1] , y+c2[2] , z+c2[3], x, y, z],  
        sphere(r2,c2)   
)$

输出——这是第一段代码的预期输出。我也尝试将变换硬编码到子程序中,但没有看到任何效果——所以是在子程序内部的变换没有起作用。

正确变换后的中心的预期输出

load(draw);
r1: 3$  /* Radius of sphere 1 */
c1: [10,10,10]$ /* Center of sphere 1 */
r2: 4$  /* Radius of sphere 2 */
c2: [5,5,5]$ /* Center of sphere 2 */

sphere(radius,coords) := block([t],
  /*   transform = [x+coords[1] , y+coords[2] , z+coords[3], x, y, z],  */
    transform = [x+10 , y+20, z+20, x, y, z],  
    spherical(radius,a,0,2*%pi,z1,0,%pi)   
)$
draw3d(
        color     = blue,    opacity   = 0.1,
/*  transform = [x+c1[1] , y+c1[2] , z+c1[3], x, y, z],   */
        sphere(r1,c1)   ,

    color     = orange,    opacity   = 0.1,
/* transform = [x+c2[1] , y+c2[2] , z+c2[3], x, y, z],  */
        sphere(r2,c2)   
)$

输出 - 球体以0,0,0为中心

以 0,0,0 为中心的球体,显示变换无效

解决方案

根据Robert Dodier的注释,在调用方把所需的一切作为一个列表([])返回

load(draw);
r1: 3$  /* Radius of sphere 1 */
c1: [10,10,10]$ /* Center of sphere 1 */
r2: 4$  /* Radius of sphere 2 */
c2: [5,5,5]$ /* Center of sphere 2 */

sphere(radius,c) := block([t],
    [transform = [x+c[1] , y+c[2] , z+c[3], x, y, z],
    spherical(radius,a,0,2*%pi,z1,0,%pi) ]  
)$
r_int :10;
draw3d(
    color= blue,  opacity   = 0.1,      sphere(r1,c1)   ,
    color= orange,opacity   = 0.1,      sphere(r2,c2)  

)$

得到正确的输出

在此输入图片描述

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章