码迷,mamicode.com
首页 > 其他好文 > 详细

地图裁剪

时间:2019-04-14 09:34:16      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:sys   threading   class   gen   drawing   app   draw   name   namespace   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string pathMap = "f:/map/0.map";
byte[,,] dataArray = this.read(pathMap);
this.write(pathMap, dataArray);
}


public void write(string pathMap, byte[,,] dataArray)
{
bool bPingxing = true;
int a0 = 352, a1 = 100, a2 = 461, a3 = 281, b0 = 312, b1 = 140;
if (bPingxing)
{
a2 = a2 - a0 + b0;
a3 = a3 - a1 + b1;
}
FileStream readStream = new FileStream(pathMap, FileMode.Open, FileAccess.ReadWrite);
byte[] data = new byte[1024];
readStream.Read(data, 0, 52);
int mapWidth = (int)data[0] + 256 * data[1];
int mapHeight = (int)data[2] + 256 * data[3];
int curWidth = 0;
int curHeight = 0;
while (curWidth < mapWidth)
{
int length = readStream.Read(data, 0, 14);
if (length == 0)
{
break;
}
else
{
if (bPingxing)
{
if(-curWidth + b1 + b0 <= curHeight && curWidth + b1 - b0 <= curHeight
&& curWidth + a3 - a2 >= curHeight && -curWidth + a3 + a2 >= curHeight)
{
//Console.WriteLine(""+ curWidth+","+ curHeight);
this.copy3To1(data, dataArray, curWidth - b0 + a0, curHeight - b1 + a1);
readStream.Seek(-14, SeekOrigin.Current);
readStream.Write(data, 0, 14);
}
}
else
{
if (curWidth >= b0 && curWidth <= (a2 - a0 + b0) && curHeight >= b1 && curHeight <= (a3 - a1 + b1))
{
this.copy3To1(data, dataArray, curWidth - b0 + a0, curHeight - b1 + a1);
readStream.Seek(-14, SeekOrigin.Current);
readStream.Write(data, 0, 14);
}
}

++curHeight;
if (curHeight >= mapHeight)
{
curHeight = 0;
++curWidth;
}
}
}
readStream.Close();
}

public byte[,,] read(string pathMap)
{
FileStream readStream = new FileStream(pathMap, FileMode.Open, FileAccess.ReadWrite);
byte[] data = new byte[1024];
readStream.Read(data, 0, 52);
int mapWidth = (int)data[0] + 256 * data[1];
int mapHeight = (int)data[2] + 256 * data[3];
byte[,,] dataArray = new byte[mapWidth, mapHeight, 14];
int curWidth = 0;
int curHeight = 0;
while (curWidth < mapWidth)
{
int length = readStream.Read(data, 0, 14);
if (length == 0)
{
break;
}
else
{
this.copy1To3(data, dataArray, curWidth, curHeight);
++curHeight;
if (curHeight >= mapHeight)
{
curHeight = 0;
++curWidth;
}
}
}
readStream.Close();
return dataArray;
}

public void copy1To3(byte[] data, byte[,,] dataArray, int curWidth, int curHeight)
{
for (int i = 0; i < 14; i++)
{
dataArray[curWidth, curHeight, i] = data[i];
}
}

public void copy3To1(byte[] data, byte[,,] dataArray, int curWidth, int curHeight)
{
for (int i = 0; i < 14; i++)
{
data[i] = dataArray[curWidth, curHeight, i];
}
}
}
}

地图裁剪

标签:sys   threading   class   gen   drawing   app   draw   name   namespace   

原文地址:https://www.cnblogs.com/agchuanqi/p/10703826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!