PDA

View Full Version : سوال: روش ترسیم اشکال گرافیکی



z_zendegi
چهارشنبه 25 شهریور 1388, 23:21 عصر
سلام دوستان
من به دنبال روشی هستم که بتونم اشکال گرافیکی(به خصوص خط) را طوری که اندازه هاش به pixel بستگی نداشته باشه(مثلا به سانتی متر بستگی داشته باشه) ترسیم کنم چون برا پرینت گرفتن به این اندازه ها احتیاج دارم.

sepehr_sepehr
پنج شنبه 26 شهریور 1388, 01:03 صبح
خوب اگه اندازه بر حسب pixel باشه با تقسیم کردن اون بر 39.37 میتونید اونو به Cm تبدیل کنید.اینجوری فکر نکنم مشکلی باشه.

z_zendegi
پنج شنبه 26 شهریور 1388, 22:33 عصر
میشه توضیح بدین که 39.37 از کجا آمده؟؟
من با پیدا کردن verticalresolution , horizental resolution تبدیلهایی انجام دادم ولی تبدیلهام فقط برای خطوط افقی و عمودی جواب میده و در مورد خطوط مورب اشتباه از اب در میاد،دلیلش را میدونم(مستطیل بودن پیکسل ها)
برا همین هم میخواستم که ترسیمم وابسته به پیکسل ها نباشه!!

sepehr_sepehr
جمعه 27 شهریور 1388, 12:39 عصر
خوب هر cm برابر با 39.37 پیکسله.
این یه قطعه کد به زبان C++:


#include "math.h"
#include <iostream.h>
#include<conio.h>
#include<graphics.h>

////////////////////////////////mehvar mokhtasat //////////////// /////
void mehvar()
{
for(int i=0;i<250;i++)
{
putpixel(250,i+250,8);
putpixel(250,250-i,8);
putpixel(250+i,250,8);
putpixel(250-i,250,8);
}

}

void DDA(float x1,float y1,float x2, float y2)
{ int dx = x2 -x1 , dy = y2 -y1 , steps;
float xIncrement=1, yIncrement=1;

if (abs(dx) > abs(dy))
steps =abs(dx);
else
steps = abs(dy);
if (dx == 0) dx = 1;
if (dy == 0) dy = 1;

xIncrement = dx /(float) steps;
yIncrement = dy /(float) steps;

putpixel((x1 + 0.5)+250,250-(y1 + 0.5), 10);
for (int i = 0; i < steps; i++)
{
x1 += xIncrement;
y1 += yIncrement;
putpixel(250+(x1 + 0.5), 250-(y1 + 0.5),10);
}

}//end of DDA

void main()
{
int n;
float x_first,x_second,y_first,y_second, finalX,finalY;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:/tc/bgi");
mehvar();

cout<<"Enter value of X1: ";
cin>>x_first;
cout<<"Enter Value of Y1: ";
cin>>y_first;

cout<<"Enter Value of nextX: ";
cin>>x_second;
cout<<"Enter Value of nextY: ";
cin>>y_second;
DDA(x_first,y_first,x_second,y_second);

getch();
closegraph();

}

اینم به زبان c#:


public partial class DDA : Form
{
int X1, Y1, X2, Y2;
bool ready;

public DDA()
{
InitializeComponent();
}

private void DDA_Load(object sender, EventArgs e)
{

}


private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
X1 = e.X;
Y1 = e.Y;
ready = true;
}

private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
ready = false;
}

private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
int x, y ;
if (ready==true)
{ x = e.X;
y = e.Y;
X2 = x;
Y2 = y;
pictureBox2.Refresh();
}
}

private void pictureBox2_Paint(object sender, PaintEventArgs e)
{

Bitmap pt = new Bitmap(1000, 1000);
int dx = X2 - X1;
int dy = Y2 - Y1, steps;
double xIncrement, yIncrement, x, y;
x = X1;
y = Y1;
if (Math.Abs(dx) > Math.Abs(dy))
steps = Math.Abs(dx);
else
steps = Math.Abs(dy);


xIncrement = dx / (float)steps;
yIncrement = dy / (float)steps;
pt.SetPixel((int)(x + 0.5), (int)(y + 0.5), Color.Green);
for (int i = 0; i < steps; i++)
{
x += xIncrement;
y += yIncrement;

pt.SetPixel((int)(x + 0.5), (int)(y + 0.5), Color.Green);

}
e.Graphics.DrawImageUnscaled(pt, 0, 0);



}


}

z_zendegi
یک شنبه 29 شهریور 1388, 00:34 صبح
این قطعه کد منه، برای ترسیم و پرینت
اگر بتونید اجراش کنید متوجه میشید که هر چه خط از حالت افقی یا عمودی فاصله میگیره بزرگتر میشه با اینکه با همون اندازه قبلی رسم شده!!
دلیلش مربع نبودن پیکسلهاست و من فکر میکردم که موقع پرینت روی کاغذ این مشکل از بین میره که نرفت!!
حالا من باید چکار کنم که بتونم خطهایی که افقی یا عمودی نیستند را هم با اندازه مشخص رسم کنم؟؟




using System;



using System.Collections.Generic;



using System.ComponentModel;



using System.Data;



using System.Drawing;



using System.Linq;



using System.Text;



using System.Windows.Forms;



using System.Windows.Media.Imaging;



using System.Drawing.Printing;




namespace Cm2Pixel



{



public partial class Form1 : Form



{



public Bitmap bmp;



public Graphics offDrawing;



public Bitmap PrintBmp = new Bitmap(300, 300);



// Create a Graphics object that is not on the form.




public Form1()



{



InitializeComponent();



}




private void button1_Click(object sender, EventArgs e)



{




bmp = new Bitmap(300,300);



offDrawing = Graphics.FromImage(bmp);



//Cm2pixel



int cm = 5;



double inch = cm / 2.54;



double pixel = inch * bmp.HorizontalResolution;



Pen pen = new Pen(Color.Black,2);



Pen pen1 = new Pen(Color.Gray, 2);



Pen pen2 = new Pen(Color.LightGray, 2);




SolidBrush brush = new SolidBrush(Color.White);




offDrawing.FillRectangle(brush, 0, 0,300, 300);



offDrawing.DrawLine(pen, 0, 50,Convert.ToInt32(pixel),50);



offDrawing.DrawLine(pen1, 50, 0, 50, Convert.ToInt32(pixel));



offDrawing.DrawLine(pen2, 20, 20, Convert.ToInt32(pixel)+ 20, Convert.ToInt32(pixel)+ 20);



offDrawing.DrawLine(pen, 70, 70, Convert.ToInt32(pixel) + 70, 50);



offDrawing.DrawLine(pen, 80, 90, Convert.ToInt32(pixel) + 80, 150);



pictureBox2.Image = bmp;




}





public void Print(Bitmap bmpForPrint)



{



PrintBmp = bmpForPrint;



PrintDocument pd = new PrintDocument();



pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);



pd.Print();



}



private void pd_PrintPage(object sender, PrintPageEventArgs ev)



{



ev.HasMorePages = false;



Bitmap bmp2 = new Bitmap(300, 300);



bmp2 = PrintBmp;



ev.Graphics.DrawImage((Image)bmp2, 0, 0);



}




private void btnPrint_Click(object sender, EventArgs e)



{



this.Print(bmp);



}



}



}