'Triangle distribution ifelse statement

I am trying to make my own function of the triangle distribution that should return the probability, similar to the one in triangle package. The problem I have is how to incorporate the if statement of lower.tail (that should compute the lower tail) and log.p (that should compute the logarithm of the density). Is there anyone who knows how to solve this?

Thanks.

The function I have:

ptriangle3 <- function(q, a, b, c, lower.tail = T, log.p = F){
  if (!all(a <= c && c <= b && a < b)) stop("Villkoren måste uppfyllas: a <= c <= b and a < b!")
  
  ifelse(q > a & q < b,
         ifelse(q <= c,
                ((q-a)^2)/((b-a)*(c-a)),
                1-((b-q)^2)/((b-a)*(b-c))),
         ifelse(q <= a,
                0,
                1))
  
}
r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source