Смекни!
smekni.com

Разработка элемента управления для отображения векторных карт (стр. 6 из 6)

{

double x,y;

x=oldDx-Convert.ToDouble(crx-oldx)/(mapD.Scale*mapD.KX);

y=oldDy-Convert.ToDouble(cry-oldy)/(mapD.Scale*mapD.KY);

mapD.SetXYAxes(x,y);

this.Refresh();

}

public void ScaleToRctangle(int oldx,int oldy,int crX,int crY)

{

double scl=0.0;

double x,y;

oldX=oldx;

oldY=oldy;

double oldKx=mapD.KX;

double oldKy=mapD.KY;

double oldSc=mapD.Scale;

int cX,cY;

if(Math.Abs(oldx-crX)>Math.Abs(oldy-crY))

{

scl=Convert.ToDouble(this.Width)/Math.Abs(oldx-crX);

}

else

{

scl=this.Height/Math.Abs(oldy-crY);

}

if(oldx<crX)

{

cX=oldx;

}

else

{

cX=crX;

}

if(oldy<crY)

{

cY=oldy;

}

else

{

cY=crY;

}

mapD.Scale=mapD.Scale*scl;

x=oldDx+Convert.ToDouble(cX)/(oldSc*oldKx);

y=oldDy+Convert.ToDouble(cY)/(oldSc*oldKy);

mapD.SetXYAxes(x,y);

this.Refresh();

}

public void VeiwInWindow()

{

mapD.SetScaleToView(this.Height,this.Width);

mapD.SetXYtoView();

this.Refresh();

}

public void RealScale()

{

mapD.Scale=1;

mapD.SetXYtoView();

this.Refresh();

}

public bool VeiwRect

{

set

{

flRec=value;

}

get

{

return flRec;

}

public int GetNamberPath (int crX, int crY)

{

double x,y;

int pth=-1;

x=oldDx+Convert.ToDouble(crX)/(this.mapD.Scale*this.mapD.KX);

y=oldDy+Convert.ToDouble(crY)/(this.mapD.Scale*this.mapD.KY);

double x1,y1,x2,y2;

double p;

double nx;

double ny=y;

int per=0;

for (int k=0;k<mapD.Length;k++)

{

per=0;

for(int j=0;j<mapD.AllPaths[k].Length-1;j++)

{

x1 = mapD.AllPaths[k].PathPoints[j].X;

y1 = mapD.AllPaths[k].PathPoints[j].Y;

x2 = mapD.AllPaths[k].PathPoints[j+1].X;

y2 = mapD.AllPaths[k].PathPoints[j+1].Y;

if(y1!=y2)

{

//this.listBox1.Items.Add("xx");

p=(ny-y2)/(y1-y2);

nx=p*x1+(1-p)*x2;

if((p>0)&&(p<1)&&(x<nx))

{

//if(nx!=x1&&ny!=y1)

per++;

if(nx==x1&&ny==y1)

{

double xp = mapD.AllPaths[k].PathPoints[j-1].X;

double yp = mapD.AllPaths[k].PathPoints[j-1].Y;

if(yp<y1&&y1<y2)

{

per++;

}

else if(y1>y2&&y1>yp)

{

else if(y1==y2&&y1==y)

{

// per++;

//невходит

}

}

//listBox1.Items.Add("per_"+per.ToString());

if((per%2)==1)

{

pth=k;

//listBox1.Items.Add("per_"+per.ToString());

//listBox1.Items.Add(k.ToString());

}

}

return pth;

}

public void Allocate(int nPth,int type)

{

mapD.Allocate(nPth,type);

this.Refresh();

}

public void SetClrPath (int NPth,Color Clr)

{

mapD.SetClrPth(NPth,Clr);

}

public Color GetClrPath(int NPth)

{

return mapD.AllPaths[NPth].ColorP;

}

public int[,] GetMsGr()

{

int [,] ms = new int [mapD.Length,mapD.Length];

for (int i=0;i<mapD.Length;i++)

{

for(int j=0;j<mapD.Length;j++)

{

ms[i,j]=0;

}

}

double x1,y1;

double _x1,_y1;

for (int i=0;i<mapD.Length;i++)

{

for(int j=0;j<mapD.AllPaths[i].Length;j++)

{

x1 = mapD.AllPaths[i].PathPoints[j].X;

y1 = mapD.AllPaths[i].PathPoints[j].Y;

for (int k=0;k<mapD.Length;k++)

{

for(int l=0;l<mapD.AllPaths[k].Length;l++)

{

if(i!=k)

{

_x1 = mapD.AllPaths[k].PathPoints[l].X;

_y1 = mapD.AllPaths[k].PathPoints[l].Y;

if(x1==_x1&&y1==_y1)

{

ms[i,k]=1;

}

return ms;

} //getmsgr

public void Run(int curPth, ref int [,] ms)

{

if(ms[curPth,curPth]==0)

{

int j=1;

bool flag = false;

while (j<256&&!flag)

{

flag=true;

for(int i=0;i<ms.GetLength(1);i++)

{

if(curPth!=i&&ms[curPth,i]>0&&ms[i,i]==j)

{

flag=false;

}

}

j++;

}

ms[curPth,curPth]=j-1;

for(int i=0;i<ms.GetLength(1);i++)

{

if(ms[curPth,i]==1&&curPth!=i)

{

ms[curPth,i]=2;

ms[i,curPth]=2;

Run(i,ref ms);

}

private void CntrIm_SizeChanged(object sender, System.EventArgs e)

{

mapD.SetScaleToView(this.Height,this.Width);

mapD.SetXYtoView();

this.Refresh();

}

public MyMap GetMap

{

get

{

return mapD;

}

}

}

}


ПриложениеБ

Текстфайла MyMap.cs

using System;

using System.Drawing;

using System.Drawing.Drawing2D;

namespace CmpView

{

/// <summary>

/// Summary description for MyMap.

/// </summary>

public class MyMap

{

private double scale;

private int length;

private int space;

private double kx;

private double ky;

private double dx;

private double dy;

private PathD [] msPthd;

private Pen [] msPn;

// private GraphicsPath[] GrphPth;

public MyMap()

{

space=20;

scale =1;

msPthd=null;

msPn=null;

kx=1;

ky=1;

dx=0;

dy=0;

//GrphPth=null;

}

public void AddPath(PathD pathD)

{

PathD[] tmpPathD = new PathD[length+1];

msPn = new Pen[length+1];

for(int i=0;i<length+1;i++)

{

msPn[i]= null;

}

if (length>0)

{

msPthd.CopyTo(tmpPathD,0);

}

tmpPathD[length]=pathD;

msPthd=tmpPathD;

tmpPathD=null;

length++;

/* Point[] tmP = new Point [pathD.Length];

for (int i=0;i<pathD.Length;i++)

{

tmP[i].X=Convert.ToInt32(pathD.PathPoints[i].X*this.scale+this.x*this.scale);

tmP[i].Y=Convert.ToInt32(pathD.PathPoints[i].Y*this.scale+this.y*this.scale);

} */

}

public PathD[] AllPaths

{

get

{

return msPthd;

}

}

public Pen[] AllPen

{

get

{

return msPn;

}

}

public int Length

{

get

{

return length;

}

}

public double Scale

{

get

{

return this.scale;

}

set

{

if(value<10000&&value>0)

{

scale=value;

}

public void SetAxes(double kX,double kY)

{

kx=kX;

ky=kY;

}

public void SetXYAxes(double dX, double dY)

{

dx=dX;

dy=dY;

}

public double KX

{

get

{

return kx;

}

}

public double KY

{

get

{

return ky;

}

}

public double DX

{

get

{

return dx;

}

}

public double DY

{

get

{

return dy;

}

}

public void SetXYtoView()

{

if(length>0)

{

double minX,minY;

minX = kx*(this.msPthd[0].PathPoints[0].X);

minY = ky*(this.msPthd[0].PathPoints[0].Y);

for (int k=0;k<length;k++)

{

for(int j=0;j<msPthd[k].Length;j++)

{

if((kx*msPthd[k].PathPoints[j].X)<minX)

{

minX=kx*msPthd[k].PathPoints[j].X;

}

if((ky*msPthd[k].PathPoints[j].Y)<minY)

{

minY=ky*msPthd[k].PathPoints[j].Y;

}

this.dx=minX/kx-space/(scale*(kx/Math.Abs(kx)));

this.dy=minY/ky-space/(scale*(ky/Math.Abs(ky)));

}

}

public void SetScaleToView(int height,int width)

{

if(length>0)

{

double minX,minY,maxX,maxY;

minX = (kx*this.msPthd[0].PathPoints[0].X);

minY = (ky*this.msPthd[0].PathPoints[0].Y);

maxX = (kx*this.msPthd[0].PathPoints[0].X);

maxY = (ky*this.msPthd[0].PathPoints[0].Y);

for (int k=0;k<length;k++)

{

for(int j=0;j<msPthd[k].Length;j++)

{

if((kx*msPthd[k].PathPoints[j].X)<minX)

{

minX=kx*msPthd[k].PathPoints[j].X;

}

if((ky*msPthd[k].PathPoints[j].Y)<minY)

{

minY=ky*msPthd[k].PathPoints[j].Y;

}

/////

if((kx*msPthd[k].PathPoints[j].X)>maxX)

{

maxX=kx*msPthd[k].PathPoints[j].X;

}

if((ky*msPthd[k].PathPoints[j].Y)>maxY)

{

maxY=ky*msPthd[k].PathPoints[j].Y;

}

double h,l;

l=Math.Abs(maxX-minX);

h=Math.Abs(maxY-minY);

if(h>l)

{

scale = (height-space*2)/h;

}

else

{

scale = (width-space*2)/l;

}

}

}

public void Allocate(int nPth,int type)

{

if(type ==0)

{

if(msPn[nPth]!=new Pen(Color.Black,4))

{

for(int i=0;i<length;i++)

{

msPn[i]= null;

}

msPn[nPth] = new Pen(Color.Black,4);

}

public void SetClrPth(int NPth, Color Clr)

{

msPthd[NPth].ColorP=Clr;

}