在坐标的相对位置显示矩阵的算法

时间:2018-08-31 19:25:36

标签: javascript algorithm multidimensional-array transform coordinate-systems

是否存在用于将空间中相对位置的矩阵进行以下转换的标准方法或算法,以if($_SERVER['REQUEST_METHOD'] == 'POST') { if(isset($_POST['submit-cover'])) { $avatarName = $_FILES['avatar']['name']; $avatarTempName = $_FILES['avatar']['tmp_name']; // List of allowed image extensions $avatarAllowedExtensions = array("jpeg","jpg","png","gif"); // Get avatar extension $avatarExtension = strtolower(end(explode('.',$avatarName))); // Check if uploaded image extension is in allowed image extensions $formErrors=array(); if(! empty($avatarName) && ! in_array($avatarExtension, $avatarAllowedExtensions)) { $formErrors[]='This extension is <strong>not allowed</strong>'; } if(empty($avatarName)) { $formErrors[]='No image <strong>uploaded</strong>'; } if(empty($formErrors)) { // Create random number between zero to million to concatinate it with image name $avatar = rand(0,1000000) . '_' . $avatarName; // Move image into Covers folder move_uploaded_file($avatarTempName, "Uploads\Covers\\" . $avatar); } } } 为原点,以向下extension=php_pdo_mysql.dll和向右x=0 and y=0为正轴。

y-axis

现在,因为此数组的长度为 5 ,并且其中最长的数组的长度为 4 ,所以我需要大小为 5 * 4的转换矩阵(采用以下格式)。

x-axis

在上述情况下,将保留相对位置。

提前谢谢!

2 个答案:

答案 0 :(得分:2)

解决方案首先将所有唯一的x值减少到排序后的平面数组中。

然后遍历每行数据,并遍历每行数组,将null拼接到孔中

let data =[ [{x:36,y:14},{x:242,y:214}],
  [{x:36,y:133}],
  [{x:36,y:252}],
  [{x:36,y:371},{x:242,y:371},{x:446,y:371},{x:651,y:371}],
  [{x:242,y:490},{x:446,y:490},{x:651,y:490}] ]
  
  
let xVals  = [...new Set(data.reduce((a,c)=>a.concat(c.map(({x})=>x)),[]))].sort((a,b)=>a-b)

data.forEach(row=>{
   xVals.forEach((x,i)=>{
      if(row[i] === undefined  || row[i].x > x){
          row.splice(i,0, null)
      }
   });
});

 data.forEach(arr=>console.log(JSON.stringify(arr)))

答案 1 :(得分:1)

检查这段代码。解释将在那里评论。

function normalize(array){

	// Get the largest sub-array. We will save this as a reference
	// to use it later
	var longest_value = array.reduce((a,b)=>a>b?a:b)


	// map each element in the main array
	return array.map(function(a){ 

		// for each item return a modified copy of the largest one.
		// To do this we map it
		return longest_value.map(function(b,i){

			// we the item with the same x position in the current main array item
			var v = a.filter(r=>r.x==b.x)

			//if there is, we return it, is not we return null
			return v.length? v[0] : null
		})
	})
}


console.log(normalize([ [{x:36,y:14},{x:242,y:214}],[{x:36,y:133}],[{x:36,y:252}],[{x:36,y:371},{x:242,y:371},{x:446,y:371},{x:651,y:371}],[{x:242,y:490},{x:446,y:490},{x:651,y:490}] ]))