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. js方法的积累

    对字符串编码解码编码  encodeURIComponet(str); 解码  decodeURIComponet(str); 及时反应的方法,监听input值是否发生改变$('#username') ...

  2. Gym 101194H / UVALive 7904 - Great Cells - [数学题+快速幂][2016 EC-Final Problem H]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  3. js 限制输入框只能输入数字的问题

    常规情况下,input设置为 type=‘number’  可以避免汉字,字符,字母,空格的输入,但是不能避免小减号 以及小键盘的减号-,加号+的输入, 可以考虑 监控 输入框的oninput事件,方 ...

  4. Xcode工程编译错误之iOS开发之Xcode9报错 Compiling IB documents for earlier than iOS7 is no longer supported.

    概要: 在我们升级到Xcode9时,最低的编译版本为iOS8,但是在使用一些SDK的时候就会报出Compiling IB documents for earlier than iOS7 is no l ...

  5. scala-协变和逆变

    class GaoJi class ZhongJi extends GaoJi //协变=========================== class Card[+T] val cgaoji = ...

  6. Ubuntu 使用命令行连接无线网

    一.查看可以使用的无线网: nmcli dev wifi 二.连接无线网: nmcli dev wifi connect ‘essid’(网络名称) password ‘password’(密码) 可 ...

  7. ThinkPHP数据库操作相关

  8. Linux+DDoS deflate 预防DDoS攻击

    使用DDoS脚本防止DDoS攻击   使用DDoS脚本防止DDoS攻击: DDoS概述: 分布式拒绝服务(DDoS:Distributed Denial of Service)攻击,指借助于客户/服务 ...

  9. java poi 合并 word文档

    import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import jav ...

  10. html5 javascript 事件练习3键盘控制练习

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...