-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathxmastree_knitr.Rmd
59 lines (40 loc) · 1.43 KB
/
xmastree_knitr.Rmd
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
---
title: "Christmas tree"
output: html_document
author: Anton Lenartovich & Agata Czajkowska
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(rbokeh)
library(dplyr)
```
```{r, warning=FALSE, message=FALSE, echo=TRUE}
getTriangle <- function(topx, topy, leftx, lefty, rightx, righty, howmuchpoint=10000){
x <- runif(howmuchpoint, leftx, rightx)
y <- runif(howmuchpoint, lefty, topy)
df <- data.frame(x = x, y= y)
a <- (topy - lefty) / (topx - leftx)
b <- topy - a*topx
df$isInside1 <- ifelse(df$x*a + b < df$y, 1, 0)
a <- (topy - righty) / (topx - rightx)
b <- topy - a*topx
df$isInside2 <- ifelse(df$x*a + b < df$y, 1, 0)
df <- df[df$isInside1 == 0 & df$isInside2 == 0,]
df
}
getTrank <- function(leftdownX, leftdownY, toprightX, toprightY){
x <- runif(10000, leftdownX, toprightX)
y <- runif(10000, leftdownY, toprightY)
df <- data.frame(x,y)
df
}
drawXMasTree <- function(){
tr1 <- getTriangle(0, 11, -1, 9, 1, 9, 1000) %>% select(x, y) %>% data.frame()
tr2 <- getTriangle(0, 9, -1.5, 6, 1.5, 6) %>% select(x, y) %>% data.frame()
tr3 <- getTriangle(0, 6, -2.5, 2, 2.5, 2) %>% select(x, y) %>% data.frame()
trank <- getTrank(-0.5, 1, 0.5, 2) %>% select(x, y) %>% data.frame()
final <- rbind(tr1, tr2, tr3, trank)
figure() %>% ly_hexbin(final$x, final$y)
}
drawXMasTree()
```