# 兩個可以自由調整的指北針

Jinlong Zhang

### 可以調整大小，以及旋轉的指北針

### location 設定指北針中心點， 的x, y, z坐標。

### size 為指北針大小的控制變量，單位為3D圖形中的1，

### rotation為指北針指向的控制變量， 單位為弧度。

northarrow <-function(location =c(0,0), size =1, rotation =0){

nnn <-par("cin")

x1 =(c(-2,0,0)*size+location[1])

y1 =c(-3.5, -2, 3.5)*size+location[2]

xx1 <-(x1-location[1])*cos(rotation)-   (y1 - location[2])*sin(rotation)+ location[1];

yy1   =(x1-location[1])*sin(rotation)+   (y1 - location[2])*cos(rotation)  +  location[2];

polygon(xx1, yy1, col=1)

x2 =c(2,0,0)*size+location[1]

y2 =c(-3.5, -2, 3.5)*size+location[2]

xx2 <-(x2-location[1])*cos(rotation)-   (y2 - location[2])*sin(rotation)+ location[1];

yy2   =(x2-location[1])*sin(rotation)+   (y2 - location[2])*cos(rotation)  +  location[2];

polygon(xx2, yy2, col=0, border =1)

return(invisible(list(xx1, yy1, xx2, yy2)))

}

plot(0:100, 0:100, pch ="")

north <- northarrow(location =c(10,10),            rotation =(pi/180)*45)

north <- northarrow(location =c(25,25),size =2,   rotation =(pi/180)*90)

north <- northarrow(location =c(50,50),size =0.5,rotation =(pi/180)*180)

north <- northarrow(location =c(75,75),size =2.5,rotation =(pi/180)*270)

######################################

northarrow.3D 可以用在三維圖中。

### location 設定指北針中心點， 的x, y, z坐標。

### pmat為persp的坐標矩陣。

### size 為指北針大小的控制變量，單位為3D圖形中的1，

### rotation為指北針指向的控制變量， 單位為弧度。

northarrow.3D <-function(location =c(80, 60, 260), pmat, size =5, rotation =0){

x1 =(c(-2,0,0)*size+location[1])

y1 =c(-3.5, -2, 3.5)*size+location[2]

xx1 <-(x1-location[1])*cos(rotation)-   (y1 - location[2])*sin(rotation)+ location[1];

yy1   =(x1-location[1])*sin(rotation)+   (y1 - location[2])*cos(rotation)  +  location[2];

north.res1 <- trans3d(x = xx1, y = yy1, z = location[3], pmat)

polygon(north.res1[[1]], north.res1[[2]], col=1)

x2 =c(2,0,0)*size+location[1]

y2 =c(-3.5, -2, 3.5)*size+location[2]

xx2 <-(x2-location[1])*cos(rotation)-   (y2 - location[2])*sin(rotation)+ location[1];

yy2   =(x2-location[1])*sin(rotation)+   (y2 - location[2])*cos(rotation)  +  location[2];

north.res2 <- trans3d(x = xx2, y = yy2, z = location[3], pmat)

polygon(north.res2[[1]], north.res2[[2]], col=0)

return(invisible(list(north.res1, north.res2)))

}

###########################################

########### 舉例###########

x <-seq(-10, 10, length=30)

y <-seq(-10, 10, length=30)

f <-function(x, y){ r <-sqrt(x^2+y^2); 10*sin(r)/r }

z <-outer(x, y, f)

z[is.na(z)]<-1

op <-par(bg ="white")

res <-persp(x, y, z, theta =30, phi =35, expand =0.5, col="grey",

ltheta =120,ticktype ="detailed",

xlab ="X", ylab ="Y", zlab ="Sinc( r )")

###

northarrow.3D(location =c(5, 5, 10), pmat = res, size =0.5,rotation =(pi/180)*45)

northarrow.3D(location =c(-5, 5, 10), pmat = res, size =0.8,rotation =(pi/180)*90)

northarrow.3D(location =c(5, -5, 10), pmat = res, size =0.7, rotation =(pi/180)*180)

northarrow.3D(location =c(-5, -5, 10), pmat = res, size =0.6, rotation =(pi/180)*270)

https://blog.sciencenet.cn/blog-255662-695244.html

## 相关博文

GMT+8, 2022-8-18 16:59