PDA

Показать полную графическую версию : [решено] Построение геом. фигуры


kadett46
20-09-2014, 22:04
Нужно построить данную фигуру (картинка).
Желательно несколькими способами. Заранее благодарен.

kadett46
20-09-2014, 22:48
1 способом сделал.
Подскажите еще.

#include "stdafx.h"
#include "iostream"
#include "conio.h"
#include "iomanip"

using namespace std;

void main()
{
int w=40,h=21;
cout << endl;
for (int i=0; i<h; i++)
{
cout << setw(21);
for (int j=0; j<w; j++)
if ( i == 0 || i == h-1 || j==0 || j==w-1)
{
cout << "*";
}
else if (j==(w-i*2))

{
cout << "*";
}
else
cout << " ";
cout << endl;
}
_getch();
}

mrcnn
21-09-2014, 16:19
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>
#include <string.h>
#include <windows.h>
#pragma comment(lib,"user32.lib")
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib,"gdi32.lib")
HRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
struct c_program
{
WNDCLASSEX wc;
MSG msg;
HWND hwnd;
HDC hdc;

void draw_gdi(){ hdc = GetDC(hwnd);

MoveToEx(hdc, 40, 40, RGB(255, 0, 0)); LineTo(hdc, 140, 40);
MoveToEx(hdc, 140, 40, RGB(255, 0, 0)); LineTo(hdc, 140, 80);
MoveToEx(hdc, 40, 40, RGB(255, 0, 0)); LineTo(hdc, 40, 80);
MoveToEx(hdc, 40, 80,RGB(255, 0, 0)); LineTo(hdc, 140, 80);
MoveToEx(hdc, 40, 80, RGB(255, 0,0)); LineTo(hdc, 140, 40);


c_program(){ set_locale(LC_ALL,"Russian"); };
~c_program(){}
int main(){ HINSTANCE hInstance=GetModuleHandle(0); LPSTR CommandLine = GetCommandLine(); return WinMain(hInstance, NULL, CommandLine, SW_SHOWDEFAULT);};
void create_wndclass(HINSTANCE hInst){ wc.cbSize=sizeof(WNDCLASSEX); wc.style=CS_HREDRAW|CS_VREDRAW; wc.lpfnWndProc=(WNDPROC)WndProc; wc.cbClsExtra=NULL; wc.cbWndExtra=NULL;wc.hInstance=hInst;wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName="MainMenu"; wc.lpszClassName="SimpleWinClass"; wc.hIcon=LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION); wc.hCursor=LoadCursor(NULL, IDC_ARROW);RegisterClassEx(&wc);}
void create_windows(HINSTANCE hInst){ hwnd=CreateWindowEx(NULL, "SimpleWinClass","host",WS_OVERLAPPEDWINDOW, 100,100,1000,1000,NULL,(HMENU) NULL,hInst, NULL); ShowWindow(hwnd, SW_SHOWNORMAL)! UpdateWindow(hwnd);}
int message_cycle(){while(GetMessage(&msg,NULL,0,0)){TranslateMessage(&msg); DispatchMessage(&msg);} return msg.wParam;}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int CmdShow){create_wndclass(hInst); create_windows(hInst); return message_cycle();}
};

c_program z;
int main(int argc, char* argv[]){ return z.main();}

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY: PostQuitMessage(NULL); return 0;
case WM_PAINT: z.draw_gdi(); return 0;
default: return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return DefWindowProc(hwnd,uMsg, wParam,lParam);
}


на GDI

pva
23-09-2014, 17:35
#include <stdio.h>
int main() {
int x1=0, x2=200, y1=0, y2=100;
printf("<svg width=\"%d\" height=\"%d\">\n"
"<polyline points=\"%d,%d %d,%d %d,%d %d,%d %d,%d %d,%d\" style=\"fill:none;stroke:black;stroke-width:3\" />\n"
"</svg>\n", x2, y2, x2,y1, x1,y2, x2,y2, x2,y1, x1,y1, x1,y2);
}


использовать так:

gcc -o test.exe test.c
test.exe > test.html
firefox test.html

pva
24-09-2014, 17:32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
int const x2=78, y2=30, pitch=x2+1, area_size=pitch*y2;

int x=1, normal=(x2-y2)/2;
char *img = malloc(area_size), *last=img+area_size-pitch, *row=last;

memset(img, ' ', area_size);

for(;;) {
if (0<normal) {
normal-=y2;
img[x]=last[x]=row[x]='x';
if (x<x2) { ++x; }
}
else {
normal+=x2;
row[0]=row[pitch-2]='x';
row[pitch-1]='\n';
if ((row-=pitch)<img) break;
}
}

fwrite(img, y2, pitch, stdout);
free(img);
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
int const x2=78, y2=30, pitch=x2+1, area_size=pitch*y2;

char *img = malloc(area_size);
unsigned const dx=x2-2, dy=y2-2;
int const width = (x2+y2)/2;
unsigned x, y;
for(y=0; y<y2; ++y) {
img[(y+1)*pitch-1] = '\n';
for(x=0; x<x2; ++x) {
img[y*pitch+x] =
x - 1 < dx && y - 1 < dy &&
width < (x-dx)*dy + y*dx ? ' ' : 'x';
}
}

fwrite(img, y2, pitch, stdout);
free(img);
}




© OSzone.net 2001-2012