distancePointLine <- function(x, y, slope, intercept) {
  ## x, y is the point to test.
  ## slope, intercept is the line to check distance.
  ##
  ## Returns distance from the line.
  ##
  ## Returns 9999 on 0 denominator conditions.
  x1 <- x-10
  x2 <- x+10
  y1 <- x1*slope+intercept
  y2 <- x2*slope+intercept
  distancePointSegment(x,y, x1,y1, x2,y2)
}

distancePointSegment <- function(px, py, x1, y1, x2, y2) {
  ## px,py is the point to test.
  ## x1,y1,x2,y2 is the line to check distance.
  ##
  ## Returns distance from the line, or if the intersecting point on the line nearest
  ## the point tested is outside the endpoints of the line, the distance to the
  ## nearest endpoint.
  ##
  ## Returns 9999 on 0 denominator conditions.
  lineMagnitude <- function(x1, y1, x2, y2) sqrt((x2-x1)^2+(y2-y1)^2)
  ans <- NULL
  ix <- iy <- 0   # intersecting point
  lineMag <- lineMagnitude(x1, y1, x2, y2)
  if( lineMag < 0.00000001) {
    warning("short segment")
    return(9999)
  }
  u <- (((px - x1) * (x2 - x1)) + ((py - y1) * (y2 - y1)))
  u <- u / (lineMag * lineMag)
  if((u < 0.00001) || (u > 1)) {
    ## closest point does not fall within the line segment, take the shorter distance
    ## to an endpoint
    ix <- lineMagnitude(px, py, x1, y1)
    iy <- lineMagnitude(px, py, x2, y2)
    if(ix > iy)  ans <- iy
    else ans <- ix
  } else {
    ## Intersecting point is on the line, use the formula
    ix <- x1 + u * (x2 - x1)
    iy <- y1 + u * (y2 - y1)
    ans <- lineMagnitude(px, py, ix, iy)
  }
  ans
}
###############################################
mydata = read.table('clipboard',header = T)
##########################
#mydata=cbind(c(1:15),wss)
#########################
datanumber = nrow(mydata)
mydist = c()
for(i in c(1:datanumber)){
  d = as.numeric(c(mydata[i,],mydata[1,],mydata[datanumber,]))
  mydist[i] = distancePointSegment(d[1],d[2],d[3],d[4],d[5],d[6])
 
}
mydist
max(mydist)
###############
#filternew = filter(mydist,filter = c(rep(1/datanumber,3)))
#plot(filternew)
################
elbowpoints = which(mydist==max(mydist))
plot(mydist)
abline(v=elbowpoints,lty=2,col='red')
plot(x=mydata[,1],y=mydata[,2],type='l')
elbowp = mydata[elbowpoints,1]
elbowp
abline(v=elbowp,lty=2,col='red')

elbow 求拐点的更多相关文章

  1. MT【293】拐点处切线

    (2018浙江高考压轴题)已知函数$f(x)=\sqrt{x}-\ln x.$(2)若$a\le 3-4\ln 2,$证明:对于任意$k>0$,直线$y=kx+a$ 与曲线$y=f(x)$有唯一 ...

  2. [LeetCode] The Skyline Problem

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  3. Java for LeetCode 218 The Skyline Problem【HARD】

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

  4. 【贪心】Vijos P1615 旅行

    题目链接: https://vijos.org/p/1615 题目大意: N条路,路的高度给你,走一条路的耗费体力是从上一条路的高度到当前路高度的绝对值差. 可以改变一条路的高度,耗费的体力等于改变前 ...

  5. bzoj3393 [Usaco2009 Jan]Laserphones 激光通讯

    Description Input 第1行输入w和H,之后W行H列输入地图,图上符号意义如题目描述. Output 最少的对角镜数量. Sample Input 7 8 ....... ...... ...

  6. 【模拟赛·polyline】

    Input file: polyline.in Output file: polyline.out Time limit: 1s Memory limit: 128M 有若⼲个类似于下⾯的函数: 定义 ...

  7. 汕头市队赛 SRM 06 B 起伏的排名

    B 起伏的排名 SRM 06 背景&&描述 天才麻将少女KPM立志要在日麻界闯出一番名堂.     在上个星期她打了n场麻将,每场麻将都有n名玩家.KPM自然记得自己的n次排名.   ...

  8. noip2013 提高组

    T1 转圈游戏 题目传送门 果不其然 第一题还是模拟题 一波快速幂解决问题 #include<cstdio> #include<cstring> #include<alg ...

  9. 花匠(codevs 3289)

    题目描述 Description 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希望剩下的花 ...

随机推荐

  1. [ERROR] - Error reading string. Unexpected token: StartObject. Path 'formData', line 1, position 13.

    公司流程框架: businessData 为 string 所有要使用JSON.stringify();

  2. C和C指针小记(十六)-动态内存分配

    动态内存分配 1.1 为什么使用动态内存分配 直接声明数组的方式的缺点: 1) 声明数组必须指定长度限制.无法处理超过声明长度的数组. 2) 如果声明更大的常量来弥补第一个缺点,会造成更多的内存浪费. ...

  3. RTOS 和中断之间要注意的

    #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY   15 #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRI ...

  4. 关于STM32时钟系统

    初学STM32,感觉最蛋疼的是它的时钟系统,每次看到它的那个时钟树就有点晕,虽然看了很多这方面的资料,甚至也已经写过很多STM32的模块代码,做过一些小项目,但一直还是对这一块模模糊糊,似懂非懂,所以 ...

  5. 在docker中使用mysql数据库,在局域网访问

    1.获取mysql镜像 docker pull mysql:5.6 注意:此处之所以获取mysql5.6是因为mysql5.7在centos7中启动可能会报错 2.查看镜像列表 docker imag ...

  6. LwIP协议栈规范翻译——摘要目录

    摘要 LwIP是一种TCP/IP协议栈的实现.LwIP协议栈专注于减少内存的使用和代码的大小,使LwIP适用于嵌入式系统中在有限的资源下能够使用小型的客户机.为了减少处理和内存的需求,LwIP使用裁剪 ...

  7. sx1278 手册参考

    记录下芯片的重要数据和内容,方便查阅,无代码实现 参考程序地址:http://www.pudn.com/Download/item/id/3070942.html  http://www.cirmal ...

  8. dubbo注册到zookeeper

    zk注册中心安装,参见dubbo官网:http://dubbo.apache.org/books/dubbo-admin-book/install/zookeeper.html provider.xm ...

  9. windows程序设计 显示一个窗口

    #include <windows.h> HINSTANCE hinst; LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM) ...

  10. POJ 2533 裸的LIS

    A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given ...