C 實現座標軸及其上的點。

2021-06-01 07:22:22 字數 3615 閱讀 6499

公司在用devexpress包,為了實現乙個類似座標的圖表,發現用devexpress比較困難,首先,其x、y軸的大小是自動的,如何設定成固定值,必須在「設計時」,而「執行時」無法更改。搞了很長一段時間都沒實現,無奈之下,自己畫乙個。實現起來還是挺難的,首先對座標值的計算,允許小於1和特別大的值,這時就需要計算x;軸或y軸應該分幾個大的刻度,每個刻度代表多少畫素。而且,這乙個大刻度又對應多少確切的數值。座標的四周允許有空白,這時又給計算座標帶來些麻煩,最終研究了半天,搞定了。如下**:

//必須傳遞的引數

float xmax = 200f; float ymax = 100f;//x軸最大值,y軸最大值

int picwidth = 410, picheight = 410;//大小

float dots = new float[3];

dots[0] = new float ;

dots[1] = new float ;

dots[2] = new float ;

pen penaxis = pens.blue;//座標軸

pen penslant = pens.green;//45度斜線

color colordot = color.red;//點

//配置引數

int top = 10, right = 10, left = 40, bottom = 40;//定義邊緣空白

int xbiglength = 6, ybiglength = 6;//大刻度線的長度

int xsmallcount = 5, ysmallcount = 5;//乙個大刻度內的小刻度個數

int xsmalllength = 3, ysmalllength = 3;//小刻度線長度

int xarrowlength = 10, yarrowlength = 10;//座標軸箭頭占用的最少座標長度

int arrowlinelength = 3;//箭頭線的長度

int dotradius = 2;//點的半徑

#region 初始化相應的類

system.drawing.bitmap bitmap = new system.drawing.bitmap(picwidth, picheight, pixelformat.format32bppargb);

system.drawing.graphics graphics = system.drawing.graphics.fromimage(bitmap);

graphics.fillrectangle(brushes.transparent, 0, 0, picwidth, picheight);

graphics.interpolationmode = system.drawing.drawing2d.interpolationmode.high;

graphics.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;

#endregion

#region 計算所需的值

int xusablelength = picwidth - left - right - xarrowlength, yusablelength = picheight - top - bottom - yarrowlength;

int xbigcount = 10, ybigcount = 10;//大刻度個數

double xrate = 1, yrate = 1;//乙個大刻度的比率,即乙個大刻度的實際數值

int xdigitlen;

if (xmax < 1)

else

int ydigitlen;

if (ymax < 1)

else

int xbigscalelen = (xusablelength / (xbigcount * xsmallcount)) * xsmallcount;//x軸方向乙個大刻度代表的畫素數

int ybigscalelen = (yusablelength / (ybigcount * ysmallcount)) * ysmallcount;//y軸方向乙個大刻度代表的畫素數

int xsmallscalelen = xbigscalelen / xsmallcount;//x軸方向乙個小刻度代表的畫素數

int ysmallscalelen = ybigscalelen / ysmallcount;//y軸方向乙個小刻度代表的畫素數

#endregion

#region 劃軸、箭頭、斜線

//x軸

graphics.drawline(penaxis, new point(left, picheight - bottom), new point(picwidth - right, picheight - bottom));

//x軸箭頭

graphics.drawline(penaxis, new point(picwidth - right, picheight - bottom), new point(picwidth - right - arrowlinelength, picheight - bottom - arrowlinelength));

graphics.drawline(penaxis, new point(picwidth - right, picheight - bottom), new point(picwidth - right - arrowlinelength, picheight - bottom + arrowlinelength));

//y軸

graphics.drawline(penaxis, new point(left, picheight - bottom), new point(left, top));

//y軸箭頭

graphics.drawline(penaxis, new point(left, top), new point(left - arrowlinelength, top + arrowlinelength));

graphics.drawline(penaxis, new point(left, top), new point(left + arrowlinelength, top + arrowlinelength));

//45%線

graphics.drawline(penslant, new point(left, picheight - bottom), new point(picwidth - right, top));

#endregion

#region 畫大刻度、小刻度

font font = new font("宋體", 9f, new fontstyle());

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

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

for (int i = 0; i < xbigcount * xsmallcount; i++)

for (int i = 0; i < ybigcount * ysmallcount; i++)

#endregion

//畫點

solidbrush dotbrush = new solidbrush(colordot);

foreach (float dot in dots)

求座標軸上的點與點之間的距離

定義一種型別 point,用於描述座標軸上的點。共同特徵 橫座標x,縱座標y 求當前點到原點 0,0 的距離?計算當前點到p1點的距離?返回兩點間p1,p2的距離?public class point public point int a public point int x,int y 返回當前點...

matlab座標軸的設定

matlab 繪圖的時候只用 plot 函式出來的圖不一定符合自己最想要的格式,經常要對座標的數字 範圍 間隔做處理。雖然不是什麼很難的操作,但是確實常用,也容易忘記,所以就放在這裡說明一下 xlabel x name x軸名稱 ylabel y name legend 線條注釋,多條的話 lege...

Qwt中座標軸的設定

1自定義座標軸 x軸設定為系統時間 分鐘,秒數 如下 class timescaledraw public qwtscaledraw 自畫座標軸 virtual qwttext label double v const 重繪座標軸 刻度值 setaxisscaledraw qwtplot xbott...