Advertisement
Guest User

knn

a guest
Mar 10th, 2022
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useRef, useEffect } from 'react';
  2. import _uniqueId from 'lodash/uniqueId'
  3.  
  4. const Canvas = props => {
  5.   const {points} = props
  6.  
  7.   const canvasRef = useRef(null)
  8.  
  9.   const draw = (ctx, start_point, scale) => {
  10.     points.forEach( item => {
  11.       item.y = parseInt(item.y) * -1; // odwracanie osi y jeśli potrzebujesz
  12.       // x = x * -1; // odwracanie osi x jeśli potrzebujesz
  13.       ctx.lineTo(item.x * scale + start_point[0], item.y * scale + start_point[1]);
  14.     })
  15.   }
  16.    
  17.   useEffect(() => {
  18.     const canvas = canvasRef.current
  19.     ,ctx = canvas.getContext('2d')
  20.  
  21.     const w = canvas.width,
  22.     h = canvas.height
  23.     const start_point = [0, h]
  24.     const scale = 30
  25.    
  26.         ctx.lineWidth=15;
  27.         ctx.strokeStyle = "#ffffff";
  28.         ctx.rect(50,50,200,150);
  29.         ctx.stroke();
  30.  
  31.     ctx.beginPath()
  32.     for (var y = 0.5; y < h; y += 10) {
  33.       ctx.moveTo(0, y);
  34.       ctx.lineTo(w, y);
  35.     }
  36.  
  37.     for (var x = 0.5; x < w; x += 10) {
  38.       ctx.moveTo(x, 0);
  39.       ctx.lineTo(x, h);
  40.     }
  41.  
  42.     ctx.strokeStyle = '#000000'
  43.     ctx.stroke()
  44.     ctx.closePath()
  45.  
  46.     ctx.beginPath()
  47.     draw(ctx, start_point, scale)
  48.     ctx.strokeStyle = 'blue'
  49.     ctx.lineWidth = 3
  50.     ctx.stroke()
  51.     ctx.closePath()
  52.  
  53.  
  54.  
  55.   }, [draw])
  56.    
  57.   return <canvas ref={canvasRef} width={400} height={400} />
  58. }
  59.  
  60. const Knn = props => {
  61.   const {select, points, startPosition} = props
  62.   if(select === "-1") return false
  63.   if(points.length <= 1) return false
  64.  
  65.   let points2 = points
  66.   let currentPoint = startPosition[0]
  67.   let usedPoints = []
  68.   let cost = 0.00
  69.  
  70.   points2 = points2.filter(point => (point.x !== currentPoint.x && point.y !== currentPoint.y))
  71.   usedPoints = usedPoints.concat(currentPoint)
  72.  
  73.   while(points2.length !== 0){
  74.     //wyliczanie wszystkich kosztów
  75.  
  76.     const costTable = points2.map( point => Math.sqrt(Math.pow(point.x - currentPoint.x, 2) + Math.pow(point.y - currentPoint.y, 2)))
  77.  
  78.     //znalezienie id najmniejszego kosztu
  79.     let min = costTable[0], id = points2[0]
  80.     for(let j=0; j<costTable.length; j++){
  81.       if(min > costTable[j]){
  82.         min = costTable[j]
  83.         id = points2[j]
  84.       }
  85.     }
  86.  
  87.     //dodanie najmniejszego kosztu do użytych
  88.     usedPoints = usedPoints.concat(id)
  89.     currentPoint = id
  90.     cost = cost + min
  91.  
  92.     //usuwanie z listy najmniejszego
  93.     points2 = points2.filter(point => point.id !== id.id)
  94.   }
  95.  
  96.   cost = Math.round(cost * 100) / 100
  97.  
  98.   const newPoints = {
  99.       id: _uniqueId(),
  100.       x: startPosition[0].x,
  101.       y: startPosition[0].y
  102.   }
  103.  
  104.   usedPoints = usedPoints.concat(newPoints)
  105.   console.log(usedPoints)
  106.  
  107.   //usedPoints = usedPoints.map(item => <li key={item.id}>Punkt ({item.x}, {item.y})</li>)
  108.  
  109.   return (
  110.     <>
  111.       <h3>Wyliczenia</h3>
  112.       <p>Koszt: {cost}</p>
  113.       <ul>
  114.         {usedPoints.map(item => <li key={item.id}>Punkt ({item.x}, {item.y})</li>)}
  115.       </ul>
  116.       <Canvas points={usedPoints}/>
  117.     </>
  118.   )
  119. }
  120.  
  121. const SelectList = props => {
  122.   const {points, HandleForm, select} = props
  123.  
  124.   if(points.length <= 2) return false
  125.  
  126.   const List = points.map(point =>
  127.     <option key={point.id} value={point.id}>({point.x}, {point.y})</option>
  128. )
  129.  
  130.   return (
  131.     <select name="select" value={select} onChange={HandleForm} >
  132.       <option key={-1} value={-1}>Wybierz</option>
  133.       {List}
  134.     </select>
  135.   )
  136. }
  137.  
  138. const ShowList = props => {
  139.   const {points, RemoveFromPoints} = props
  140.  
  141.   if(points.length <= 0) return false
  142.  
  143.   return (
  144.     points.map(point => <li key={point.id}>Punkt: ({point.x}, {point.y}) <button onClick={RemoveFromPoints.bind(this, point.id)}>Usuń</button></li>)
  145.   )
  146. }
  147.  
  148. const InsertForm = props => {
  149.   const {HandleForm, AddToPoints} = props
  150.  
  151.   return (
  152.     <>
  153.       <h1>Algorytm najbliższego sąsiada</h1>
  154.       <p>Podaj x: <input name="x" type="text" onChange={HandleForm}/></p>
  155.       <p>Podaj y: <input name="y" type="text" onChange={HandleForm}/></p>
  156.       <button onClick={AddToPoints}>Dodaj</button>
  157.     </>
  158.   )
  159. }
  160.  
  161. class App extends React.Component {
  162.   state = {
  163.     select: "-1",
  164.     x: 0,
  165.     y: 0,
  166.     cost: 0,
  167.     points: [],
  168.     startPosition: {},
  169.     knnPoints: []
  170.   }
  171.  
  172.   SetStartPosition = id => {
  173.     const startPosition = this.state.points.filter(points => {
  174.       return points.id === id
  175.     })
  176.  
  177.     this.setState({startPosition})
  178.   }
  179.  
  180.   HandleForm = e => {
  181.     const {name, value} = e.target
  182.     this.setState({[name]: value})
  183.  
  184.     if(name === 'select' && value !== "-1"){
  185.       this.SetStartPosition(value)
  186.     }
  187.   }
  188.  
  189.   AddToPoints = () => {
  190.     const newPoints = [{
  191.       id: _uniqueId(),
  192.       x: this.state.x,
  193.       y: this.state.y
  194.     }]
  195.  
  196.     this.setState({
  197.       points: this.state.points.concat(newPoints)
  198.     })
  199.   }
  200.  
  201.   RemoveFromPoints = id => {
  202.     const points = this.state.points.filter(points => {
  203.       return points.id !== id
  204.     })
  205.  
  206.     this.setState({points})
  207.   }
  208.  
  209.   render() {
  210.     return (
  211.       <React.StrictMode>
  212.         <InsertForm
  213.           HandleForm={this.HandleForm}
  214.           AddToPoints={this.AddToPoints}
  215.         />
  216.         <ShowList
  217.           points={this.state.points}
  218.           RemoveFromPoints={this.RemoveFromPoints}
  219.         />
  220.         <SelectList
  221.           points={this.state.points}
  222.           HandleForm={this.HandleForm}
  223.           select={this.state.select}
  224.         />
  225.         <Knn
  226.           select={this.state.select}
  227.           points={this.state.points}
  228.           startPosition={this.state.startPosition}
  229.         />
  230.       </React.StrictMode>
  231.     )
  232.   }
  233. }
  234.  
  235. export default App;
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement