Skip to content

[Bug fixes] Depth sorting problem? #1053

@hexiongwu1995

Description

@hexiongwu1995

According to the code settings, the x-axis and y-axis should have emerged from the central plane of the Torus, but in reality, they are emerging from the bottom of the Torus. The correspondence between the length of the axes and the diameter of the Torus is also inaccurate.

I can't determine the cause. Maybe it's a sorting problem or a bug?

#set page(paper:"a4", flipped:true, margin:1cm)
#set align(center+horizon)

#import "@preview/cetz:0.4.2"
#cetz.canvas({
import cetz.draw:*
import cetz.angle:*
import calc:*
import cetz.util.vector

let sph(r, theta, phi)={
let x=r* sin(theta)* cos(phi)
let y=r* sin(theta)* sin(phi)
let z=r*cos(theta)
return (x,y,z) }

scale(x:1, y:1, z:1)
set-style(mark:(end:(symbol:"stealth", fill:black, scale:0.5)))

ortho(x:-75deg, y:0deg, z:-120deg,{

line((0,0,0), (7,0,0), name:"x", stroke:(dash:"dotted") ) 
line((0,0,0), (0,6,0), name:"y", stroke:(dash:"dotted") ) 
line((0,0,0), (0,0,4), name:"z", stroke:(dash:"dotted") ) 
content((rel:(0.3,0,0), to: "x.end"), $x$) 
content((rel:(0,0.3,0), to: "y.end"), $y$) 
content((rel:(0,0,0.3), to: "z.end"), $z$) 

let Draw-torus(R:4, r:0.6, Phi-num:30, Polar-num:30, Fill:blue, Stroke:none,  Plight:(1,0,0.3), Pintensity:0.5, Aintensity:0.2)= {

let Get-Coords(Phi,Polar)={
let x= (R+ r*cos(Polar))*cos(Phi)
let y= (R+ r*cos(Polar))*sin(Phi)
let z= r*sin(Polar)
return (x,y,z)
}

for i in range(Phi-num) {
for j in range(Polar-num) {

let Phi-step= 2*pi/Phi-num
let Phi-curr= i* Phi-step
let Phi-next= (i+1)* Phi-step

let Polar-step= 2*pi/Polar-num
let Polar-curr= j* Polar-step
let Polar-next= (j+1)* Polar-step

let Pa= Get-Coords(Phi-curr, Polar-curr)
let Pb= Get-Coords(Phi-curr, Polar-next)
let Pc= Get-Coords(Phi-next, Polar-next)
let Pd= Get-Coords(Phi-next, Polar-curr)

let rect-diagonal-a = vector.sub(Pa, Pc)
let rect-diagonal-b = vector.sub(Pb, Pd)
let normal-of-rect= vector.cross(rect-diagonal-a, rect-diagonal-b)

let rect-center= vector.scale(vector.add(Pa, Pc),0.5)
let tube-center= sph(R, pi/2, (Phi-curr+ Phi-next)/2)
let tubecenter-to-rectcenter= vector.sub( rect-center, tube-center )
let Outward-normal-of-rect= { 
let dot-product= vector.dot(normal-of-rect,tubecenter-to-rectcenter);
if dot-product < 0 { vector.scale(normal-of-rect,-1)} else { normal-of-rect}
}

let normalize-of-rect= vector.norm(Outward-normal-of-rect)
let normalize-of-Plight= vector.norm(Plight)
let P-Diffuse= max(0,vector.dot(normalize-of-rect, normalize-of-Plight))

let Intensity= min(1,Pintensity * P-Diffuse + Aintensity)

let Fill-lighten = {if type(Fill)==color { Fill.lighten(100% * Intensity )} else { Fill} }

line(Pa, Pb, Pc, Pd, close:true, fill:Fill-lighten, mark:none, stroke: Stroke )

} } } 

Draw-torus()

}) })
Image

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions