两个可以自由调整的指北针

用R绘制地图,或者三维图时, 有时候需要添加指北针, 但是很多时候很难进行角度调整等。 这里提供了两个绘制指北针的函数。 可以调整中心点坐标, 指北针的大小,以及旋转的角度, 分别用于二维平面作图和三维作图。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
\### 可以调整大小,以及旋转的指北针
\### 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)

img
img