张金龙的博客分享 http://blog.sciencenet.cn/u/zjlcas 物种适应性、分布与进化

博文

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

已有 4888 次阅读 2013-5-31 11:47 |个人分类:统计分析|系统分类:科研笔记| 指北針, R語言

兩個自由調整的指北針

Jinlong Zhang


用R繪製地圖,或者三維圖時, 有時候需要添加指北針, 但是很多時候很難進行角度調整等。 這裡提供了兩個繪製指北針的函數。 可以調整中心點坐標, 指北針的大小,以及旋轉的角度, 分別用於二維平面作圖和三維作圖。


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

### 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://wap.sciencenet.cn/blog-255662-695244.html

上一篇:样地数据从海拔计算坡度、坡向和凹凸度
下一篇:Rcpp简明指南: R调用C++
收藏 IP: 202.64.82.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-12-26 19:52

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部