IPB
ЛогинПароль:

> Внимание!

1. Пользуйтесь тегами кода. - [code] ... [/code]
2. Точно указывайте язык, название и версию компилятора (интерпретатора).
3. Название темы должно быть информативным.
В описании темы указываем язык!!!

Наладить общение поможет, если вы подпишитесь по почте на новые темы в этом форуме.

 
 Ответить  Открыть новую тему 
> Рисование графика
сообщение
Сообщение #1


Perl. Just code it!
******

Группа: Пользователи
Сообщений: 4 100
Пол: Мужской
Реальное имя: Андрей

Репутация: -  44  +


Вот решил сделать на Си. Это первый вариант - простой, по поводу следующего есть много идей, надеюсь сделаю, выложу.


#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>

int OpenGraph();
int CloseGraph();
void ShowDecart(int X2, int Y2);
int GetX(int X2, double x);
int GetY(int Y2, double y);
double Function(double x);

void Shedule(double a, double b, int X2, int Y2);

int main (void){

OpenGraph();

const int X = getmaxx();
const int Y = getmaxy();
const int X2 = X / 2;
const int Y2 = Y / 2;

ShowDecart(X2, Y2);
Shedule(-100, 100, X2, Y2);

getche();

CloseGraph();

return 0;
}

double Function(double x){
return fabs(x);
}

void Shedule(double a, double b, int X2, int Y2){
double x = a;
double step = .1;
while (x <= b + step / 2){
putpixel(GetX(X2, x), GetY(Y2, Function(x)), RED);
x += step;
}
}


int OpenGraph(){
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\tc\\tc\\bgi");
return graphresult();
}

int CloseGraph(){
closegraph();
return graphresult();
}

void ShowDecart(int X2, int Y2){

int delta = 200;
int d = delta / 10;

line(X2 - delta, Y2, X2 + delta, Y2);
line(X2, Y2 - delta, X2, Y2 + delta);

int i = X2 - delta;

while (i <= X2 + delta){
if ((i > X2) && (i < X2 + delta)){
char *s;
itoa((i - X2) / d, s, 10);
outtextxy(i - 3, Y2 + 5, s);
}
circle(i, Y2, 1);
i += d;
};


i = Y2 - delta;

while (i <= Y2 + delta){
if ((i > Y2) && (i < Y2 + delta)){
char *s;
itoa((i - Y2)/ d, s, 10);
outtextxy(X2 - 10, i - 3, s);
}
circle(X2, i, 1);
i += d;
};

}

int GetX(int X2, double x){
return X2 + int(x);
}

int GetY(int Y2, double y){
return Y2 - int(y);
}


Может кому пригодится smile.gif


--------------------
perl -e 'print for (map{chr(hex)}("4861707079204E6577205965617221"=~/(.{2})/g)), "\n";'
 Оффлайн  Профиль  PM 
 К началу страницы 
+ Ответить 
сообщение
Сообщение #2


Perl. Just code it!
******

Группа: Пользователи
Сообщений: 4 100
Пол: Мужской
Реальное имя: Андрей

Репутация: -  44  +


Вот и версия поинтереснее, единственное, я думаю еще можно что-то подумать по поводу масштабирования, а так, благодаря возможности описывать указатель на функцию, очень просто отобразить сразу несколько разных графиков :


#include <graphics.h>
#include <conio.h>
#include <stdlib.h>

#include <iostream.h>

class Shedule{

public :

Shedule();

void Decart();
void Show(int color, double a, double b, double step);
int GetX(double x);
int GetY(double y);

int (*Function) (double x);

private :

int X2, Y2, delta, d;

};

Shedule :: Shedule(){
X2 = getmaxx() / 2;
Y2 = getmaxy() / 2;
delta = 200;
d = delta / 10;
};


int Shedule :: GetX(double x){
return X2 + int(x);
};

int Shedule :: GetY(double y){
return Y2 - int(y);
};

void Shedule :: Decart(){

line(X2 - delta, Y2, X2 + delta, Y2);
line(X2, Y2 - delta, X2, Y2 + delta);

int i = X2 - delta;

while (i <= X2 + delta){
if ((i > X2) && (i < X2 + delta)){
char *s;
itoa((i - X2) / d, s, 10);
outtextxy(i - 3, Y2 + 5, s);
}
circle(i, Y2, 1);
i += d;
};

i = Y2 - delta;

while (i <= Y2 + delta){
if ((i > Y2) && (i < Y2 + delta)){
char *s;
itoa((i - Y2)/ d, s, 10);
outtextxy(X2 - 10, i - 3, s);
}
circle(X2, i, 1);
i += d;
};

};

int Shedule :: *Function (double x){
return Function(x);
};

void Shedule :: Show(int color, double a, double b, double step){
double x = a;
while (x <= b + step / 2){
putpixel(GetX(x), GetY(Function(x)), color);
x += step;
}
};


int OpenGraph(char *s){

int gd = DETECT, gm;

initgraph(&gd, &gm, s);

return graphresult();
}

int CloseGraph(){
closegraph();
return graphresult();
}




#include "klem4.h"
#include <math.h>

int Func1(double x){
return x > 0 ? int(sqrt(x)) : 0;
}

int Func2(double x){
return x > 0 ? int(x) : int(-x);
}

int Func3(double x){
return 10 * (cos(0.5 * x));
}


int main (void){

OpenGraph("c:\\tc\\tc\\bgi");

Shedule SH;
SH.Decart();

SH.Function = Func1;
SH.Show(RED, 0, 100, 0.1);

SH.Function = Func2;
SH.Show(BLUE, -100, 100, 0.1);

SH.Function = Func3;
SH.Show(GREEN, 100, 200, 0.01);

getche();
CloseGraph();

return 0;
}


--------------------
perl -e 'print for (map{chr(hex)}("4861707079204E6577205965617221"=~/(.{2})/g)), "\n";'
 Оффлайн  Профиль  PM 
 К началу страницы 
+ Ответить 
сообщение
Сообщение #3


Гость






klem4, а зачем ты выделил отдельно
int (*Function) (double x); ? Может, есть смысл сделать так:

void Shedule :: Show(int color, double a, double b, double step,
int (*F)(double x)) {

double x = a;
while (x <= b + step / 2){
putpixel(GetX(x), GetY(F(x)), color);
x += step;
}
};


и вызывать так:
  SH.Show(RED, 0, 100, 0.1, Func1);
SH.Show(BLUE, -100, 100, 0.1, Func2);
SH.Show(GREEN, 100, 200, 0.01, Func3);

?
 К началу страницы 
+ Ответить 
сообщение
Сообщение #4


Perl. Just code it!
******

Группа: Пользователи
Сообщений: 4 100
Пол: Мужской
Реальное имя: Андрей

Репутация: -  44  +


Да, так конечно лучьше smile.gif) Просто первый раз указатель на ф-ю юзаю smile.gif Не догадался чо как можно сделать smile.gif


--------------------
perl -e 'print for (map{chr(hex)}("4861707079204E6577205965617221"=~/(.{2})/g)), "\n";'
 Оффлайн  Профиль  PM 
 К началу страницы 
+ Ответить 

 Ответить  Открыть новую тему 
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0

 





- Текстовая версия 29.03.2024 7:10
500Gb HDD, 6Gb RAM, 2 Cores, 7 EUR в месяц — такие хостинги правда бывают
Связь с администрацией: bu_gen в домене octagram.name